Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
# n, m = map(int, input().split()) # l1 = list(map(int, input().split())) # l2 = list(map(int, input().split())) # l3 = [l1[0]] # # for i in range(1, n): # l3.append(l3[i-1]+l1[i]) # # # def how_many_warriors_die(l3, n, e): # l, r = 0, n-1 # last = n # # while l <= r: # mid = (l+r)//2 # if l3[mid] > e: # r = mid - 1 # if mid < last: # last = mid # else: # l = mid + 1 # # return last # # # for e in l2: # print(how_many_warriors_die(l3, n, e)) n = int(input()) l1 = list(map(int, input().split())) m = int(input()) l2 = list(map(int, input().split())) l3 = [l1[0]] for i in range(1, n): l3.append(l3[i - 1] + l1[i]) def bin_search(l3, n, e): l, r = 0, n - 1 last = n while l <= r: mid = (l + r) // 2 if l3[mid] > e: r = mid - 1 if mid < last: last = mid else: l = mid + 1 if l3[last-1] == e: return last return last+1 for e in l2: print(bin_search(l3, n, e))
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; vector<int> v; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) v.push_back(a[i]); else { v.push_back(v[v.size() - 1] + a[i]); } } int m, temp; cin >> m; for (int i = 0; i < m; i++) { cin >> temp; vector<int>::iterator low = lower_bound(v.begin(), v.end(), temp); cout << (low - v.begin()) + 1 << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from bisect import bisect_left a = int(input()) b = list(map(int,input().split())) c = input() d = list(map(int,input().split())) temp = [1,b[0]] for i in range(1,a):#Implement piles temp.append(temp[len(temp)-1]+1) temp.append(temp[len(temp)-1]+b[i]-1) upperBound = [] for i in range(1,len(temp),2):#Get upperbounds only upperBound.append(temp[i]) hold = upperBound for i in d: print(bisect_left(hold,i)+1)#Binary insert
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; inline int cmp(double x, double y = 0, double tol = 1e-10) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int main() { int n; scanf("%d", &n); int v[n]; for (int i = 0; i < n; i++) scanf("%d", &v[i]); vector<pair<int, int> > seg; seg.push_back({1, v[0]}); for (int i = 1; i < n; i++) seg.push_back({seg[i - 1].second + 1, seg[i - 1].second + v[i]}); int q, x; scanf("%d", &q); while (q--) { scanf("%d", &x); int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; if (seg[mid].first <= x && x <= seg[mid].second) { printf("%d\n", mid + 1); break; } if (seg[mid].first > x) r = mid - 1; else l = mid + 1; } } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from bisect import bisect_left n = int(input()) a = list(map(int, input().split())) m = int(input()) q = list(map(int, input().split())) piles = [] first = 0 for i in a: piles.append(first+i) first += i for i in q: print(bisect_left(piles, i)+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#!/usr/bin/env python n = input() piles = map(int, raw_input().split(' ')) all_worms = list() for i in range(len(piles)): all_worms += [i+1]*piles[i] #print all_worms #exit() m = input() jucy = map(int, raw_input().split(' ')) output = list() for j in jucy: output.append(all_worms[j-1]) for val in output: print val
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int a[100005]; pair<int, int> p[200050]; int find(int t, int n) { int l, r, m; l = 0, r = n; while (1) { m = (l + r) / 2; if (p[m].first <= t && p[m].second >= t) { return m + 1; } if (p[m].first > t) r = m; else l = m; } } int main() { int n, m; cin >> n; int l = 1; for (int i = 0; i < n; i++) { cin >> a[i]; p[i].first = l; p[i].second = l + a[i] - 1; l = l + a[i]; } cin >> m; int q; while (m--) { cin >> q; cout << find(q, n) << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int CUMULATIVE[300009]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; bool isValid(int x, int y, int n, int m) { return x >= 0 && y >= 0 && x < n && y < m; } void debugMatrix(int X[][300009], int n, int m) { int i, j; for ((i) = (0); (i) <= (n - 1); (i) += 1) { for ((j) = (0); (j) <= (m - 1); (j) += 1) printf("%3d ", X[i][j]); cout << endl; } } void debugArray(int X[], int n) { int i; for ((i) = (1); (i) <= (n); (i) += 1) cout << X[i] << " "; cout << endl; } void debugVar(string preFix, int x) { cout << preFix << " " << x << endl; } void INIT(int n) { int i, j; for ((i) = (1); (i) <= (n); (i) += 1) { } } int SOLVE(int n) { int i, ans = 0, j; return ans; } int main() { int max_cases, n, i, m, k, j, x, q, pileNum; while (cin >> n) { memset((CUMULATIVE), 0, sizeof(CUMULATIVE)); for ((i) = (1); (i) <= (n); (i) += 1) { cin >> x; CUMULATIVE[i] = (i == 1) ? x : CUMULATIVE[i - 1] + x; } cin >> m; for ((i) = (1); (i) <= (m); (i) += 1) { cin >> q; pileNum = lower_bound(CUMULATIVE + 1, CUMULATIVE + n + 1, q) - (CUMULATIVE); cout << pileNum << endl; } } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from sys import stdin from bisect import bisect_left as bisect from itertools import accumulate test_case = stdin.readlines() worms = [int(c) for c in test_case[1].split()] queries = [int(c) for c in test_case[3].split()] worms_acc = list(accumulate(worms)) ans = [bisect(worms_acc, q) + 1 for q in queries] print('\n'.join(map(str, ans)))
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect n = int(raw_input()) a = map(int, raw_input().split()) m = int(raw_input()) q = map(int, raw_input().split()) cs = [0] * n cs[0] = a[0] for i in xrange(1, n): cs[i] = cs[i - 1] + a[i] for i in xrange(m): print bisect.bisect_right(cs, q[i] - 1) + 1
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
def bin(a, x): l = -1 r = len(a) while r > l + 1: k = (r + l) // 2 if a[k] >= x: r = k else: l = k return r n = int(input()) a = [int(i) for i in input().split()] for i in range(1, len(a)): a[i] += a[i - 1] m = int(input()) b = [int(i) for i in input().split()] for i in b: print(bin(a, i) + 1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import sys from itertools import accumulate def write_to_stdout(res): sys.stdout.write('%s\n' % '\n'.join(res)) def read_input(): n = int(input()) worms = [int (_) for _ in input().rstrip().split()] temp = input() idxes = [int (_) for _ in input().rstrip().split()] return worms, idxes def get_idx(number, worms_cumul, start, end): while start <= end: index = (end + start) // 2 current_value = worms_cumul[index] if current_value == number or start == end: return index+1 elif current_value > number: end = index else: start = index+1 return end+1 def get_idx2(number, worms_cumul, start, end): for idx, num in enumerate(worms_cumul): if num >= number: return idx+1 return 0 def get_answer(worms, numbers): worms_cumul = list(accumulate(worms)) ans = [] for n in numbers: idx = get_idx(n, worms_cumul, 0, len(worms_cumul)-1) ans.append(str(idx)) return ans def main(): worms, idxes = read_input() # worms, idxes = [2, 7, 3, 4, 9], [1, 25, 11] answer = get_answer(worms, idxes) write_to_stdout(answer) if __name__ == '__main__': main()
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) a = list(map(int,input().split())) m = int(input()) q = list(map(int,input().split())) b = [0]*sum(a) c = 0 for i in range(len(a)): b[c:c+a[i]] = [i]*(a[i]) c += a[i] for i in q: print(b[i-1]+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n=int(input()) a=list(map(int,input().split())) m=int(input()) q=list(map(int,input().split())) b=[] for i in range(n): b+=[i+1]*a[i] # print(b) for i in q: print(b[i-1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(raw_input()) lst = [int(x) for x in raw_input().split(' ')] m = int(raw_input()) q = [int(x) for x in raw_input().split(' ')] num = [0] for i in range(0, len(lst)): num.append(lst[i]+num[i]) for obj in q: lft, rgh = 0, len(num)-1 while lft + 1 < rgh: mid = (lft + rgh) / 2 if obj > num[mid]: lft = mid else: rgh = mid print rgh
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) a[i] += a[i - 1]; int q; cin >> q; for (int i = 0; i < q; i++) { int x; cin >> x; int val = lower_bound(a, a + n, x) - a; cout << val + 1 << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Atanas */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int N = in.nextInt(); long[] pref = new long[N + 1]; for (int i = 1; i <= N; ++i) { pref[i] = in.nextInt(); pref[i] += pref[i - 1]; } int Q = in.nextInt(); for (int q = 0; q < Q; ++q) { long target = in.nextLong(); int low = 1; int high = N; int best = N; while (low <= high) { int mid = low + (high - low) / 2; if (pref[mid] >= target) { best = Math.min(best, mid); high = mid - 1; } else low = mid + 1; } out.println(best); } } } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import sys def search(Max,new,num): if new[0]>num: return 1 Min =0 Mid = int((Max+Min)/2) while Min<=Max: if new[Mid]==num: return Mid+1 break elif new[Min]==num: return Min+1 break elif new[Mid]<num and new[Mid+1]>num: return Mid+2 break elif new[Mid]<num: Min=Mid+1 else: Max=Mid-1 Mid = Min + (Max-Min) //2 n =int(sys.stdin.readline()) a=map(int, sys.stdin.readline().split()) m= int(sys.stdin.readline()) mq=map(int, sys.stdin.readline().split()) new=[a[0]] for x in xrange(1,n): new.append(new[x-1]+a[x]) #print new #print mq rep ="" for x in mq: rep+=str(search(n,new,x))+'\n' print rep[:-1]
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect my_list = [] new_list = [] find_list = [] num = int(input()) my_list = list(map(int,input().split())) a = 1 new_list.append(my_list[0]) new_list.append(my_list[0]+a+(my_list[1]-1)) for i in range(1,len(my_list)-1,1): k = i+1 new_list.append(new_list[i]+a+(my_list[k]-1)) num = int(input()) find_list = list(map(int,input().split())) for i in range(num): val = bisect.bisect(new_list,find_list[i]) if(find_list[i]==new_list[val-1]): print(val) else: print(val+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
def binarySearch(L, num): i = 0 j = len(L) mid = (i + j) // 2 out = -1 while True: # print(i, j, mid) if i > j or mid >= len(L): break elif L[mid] >= num: out = mid j = mid - 1 elif L[mid] < num: i = mid + 1 mid = (i + j) // 2 return out raw_input() line = raw_input() tokes = line.split() a = [0] for i in range(len(tokes)): a.append(a[i] + int(tokes[i])) raw_input() tokes = raw_input().split() w = [int(t) for t in tokes] for worm in w: print binarySearch(a, worm)
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
# Worms n = int(input()) a = list(map(int, input().split())) m = int(input()) q = list(map(int, input().split())) suma = [a[0]] i = 1 while i < n: suma.append(suma[i-1] + a[i]) i += 1 for item in q: high = n-1 low = 0 mid = (high+low)//2 while True: if item > suma[mid]: low = mid + 1 mid = (high+low)//2 elif item < suma[mid]: if mid == 0: break else: if item > suma[mid -1]: break else: high = mid- 1 mid = (high+low)//2 else: break print(mid+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) bunch = list(map(int, input().split())) w = int(input()) worms = list(map(int, input().split())) list = [] k = 1 for i in bunch: list += [k] * i k += 1 for i in worms: print(list[i - 1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; struct node { int id, x; }; bool operator<(node a, node b) { if (a.x < b.x) return 1; else return 0; } int main() { int n, m, a[100009]; node b[100009]; map<int, int> ma; cin >> n; a[0] = 1; n += 1; for (int i = 1; i < n; i++) cin >> a[i]; a[n] = 1000009; int y = 0; for (int i = 2; i < n; i++) { a[i] += a[i - 1]; } cin >> m; int x; for (int i = 0; i < m; i++) { cin >> x; b[i].x = x; b[i].id = i; } sort(b, b + m); int j = 1; for (int i = 0; i < m; i++) { for (; j <= n;) { if (b[i].x <= a[j]) { ma[b[i].id] = j; break; } else j++; } } for (int i = 0; i < m; i++) { cout << ma[i] << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
num_heaps = int(input()) worms_per_heap = [int(x) for x in input().split()] worm = 1 wpdict = {} for heap in range(1, num_heaps + 1): for i in range(1, worms_per_heap[heap - 1] + 1): wpdict[worm] = heap worm += 1 num_tasty_worms = int(input()) tasty_worms = [int(x) for x in input().split()] for worm in tasty_worms: print(wpdict[worm])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = input() a = map(int,raw_input().split()) q = input() m = map(int,raw_input().split()) f = [0] for i in range(n): f += [i+1]*a[i] for x in m: print f[x]
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
//474B : WORMS /* * Solution: */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; //Your code goes here public class Main { static class ProblemSolver{ public void solveTheProblem(InputReader in,PrintWriter out){ /* * This question need an approach where we need to * solve it in a systematic manner along with the inputs. * We need to construct a lookup table in order to solve * this issue. As we know that every worm can be mapped with * one of the pile we should do that at the beginning only. * Then it is a matter on O(1) search. This algorithm decreases * the time complexity like anything. It also denotes that * sometimes we need to trade space to make a program time * efficient. */ int piles=in.nextInt(); int[] look=new int[1000005]; int j=1; for(int i=1;i<=piles;++i){ int up=in.nextInt(); while(up>0){ look[j]=i; --up; ++j; } } int fav=in.nextInt(); int[] ans=new int[fav]; for(int i=0;i<fav;++i){ int idx=in.nextInt(); ans[i]=look[idx]; } for(int i=0;i<fav;++i)System.out.println(ans[i]); } } //Default template for all the codes static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream),32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } //Main method for all the codes public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemSolver problemSolver = new ProblemSolver(); problemSolver.solveTheProblem(in, out); out.close(); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
# -*- coding: utf-8 -*- """ Created on Sun Aug 30 21:37:23 2020 @author: dehan """ n = input("") a = list(map(int,input("").split())) m = int(input("")) q = list(map(int,input("").split())) worm_dict = dict() total = 0 pileId = 0 for i in range(max(q)+1): if(i>total): total+=a[pileId] pileId += 1 d = {i:pileId} worm_dict.update(d) #print(worm_dict) for label in q: print(worm_dict[label]) """ startPiles = [0] it = 0 for worms in a: startPiles.append(startPiles[it] + worms) it+=1 for label in q: lowerId = 0 upperId = len(startPiles) while(True): pileId = int((upperId+lowerId)/2) if(label<=startPiles[pileId]): if(startPiles[pileId-1]<label): break upperId = pileId else: lowerId = pileId+1 print(pileId) """
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; void pause() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); unsigned long int n, a[100005], m, q[100005]; scanf("%d", &n); unsigned int i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d", &q[i]); } unsigned long int s[100005], e[100005]; s[0] = 1, e[0] = a[0]; for (i = 1; i < n; i++) { s[i] = s[i - 1] + a[i - 1]; e[i] = e[i - 1] + a[i]; } for (i = 0; i < m; i++) { unsigned int l = 0, h = n - 1, pos; while (l <= h) { unsigned int mid = (l + h) / 2; if (s[mid] == q[i]) { pos = mid; break; } else if (s[mid] < q[i]) { if (e[mid] >= q[i]) { pos = mid; break; } else l = mid + 1; } else h = mid - 1; } printf("%d\n", pos + 1); } pause(); return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, a, p = 0; scanf("%d", &n); vector<int> v; for (int i = 0; i < n; i++) { scanf("%d", &a); v.push_back(p + a); p += a; } vector<int>::iterator it; int q, x; scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d", &x); it = lower_bound(v.begin(), v.end(), x); cout << (it - v.begin()) + 1 << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.util.*; import java.io.*; public class marmot { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n+1]; int[] prefix = new int[n+1]; for(int i=1;i<n+1;i++) { arr[i] = in.nextInt(); } prefix[1] = arr[1]; for(int i=2;i<n+1;i++) { prefix[i]=prefix[i-1]+arr[i]; } int m = in.nextInt(); for(int i=0;i<m;i++) { int loc = 0; int a = in.nextInt(); loc = Arrays.binarySearch(prefix,a); if(loc<0) loc = loc*-1-1; System.out.println(loc); } } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) a = list(map(int, input().split())) m = int(input()) q = list(map(int, input().split())) for i in range(1, n): a[i] += a[i - 1] for x in q: left = 0 right = n while left < right: mid = (left + right) // 2 if x > a[mid]: left = mid + 1 elif x <= a[mid] and (mid == 0 or x > a[mid - 1]): print(mid + 1) break else: right = mid
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n,a = input(),map(int, raw_input().split()) m,q = input(),map(int, raw_input().split()) dp = [] for i in xrange(n): dp += [i+1]*a[i] for x in q: print dp[x-1]
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import os import sys from collections import Counter from io import BytesIO, IOBase import bisect as bi BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def main(): t=1 for _ in range(t): n=int(input()) a=list(map(int,input().split())) for i in range(1,len(a)): a[i]+=a[i-1] m=int(input()) q=list(map(int,input().split())) for i in q: print(bi.bisect_left(a,i)+1) if __name__ == "__main__": main()
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) a = list(map(int, input().split(' '))) m = int(input()) q = list(map(int, input().split(' '))) for i in range(1, n): a[i] = a[i] + a[i - 1] l = 0 k = [k for k in range(1000000)] for i in range(n): for j in range(l, a[i]): k[j] = i + 1 l = a[i] for i in range(m): print(k[q[i] - 1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import math n=int(input()) a=[int(x) for x in input().split()] m=int(input()) q=[int(x) for x in input().split()] label=[0] for i in a: label.append(label[-1]+i) for i in q: low=0 high=n while True: mid=math.floor((low+high)/2) if i>label[mid-1] and i<=label[mid]: print(mid) break elif i>label[mid]: low=mid+1 else: high=mid-1
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.BufferedReader; import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; /** * Author: Luis Manuel Díaz Barón * Problem: * Online Judge: COJ * Idea: * */ public class Main { static MyScanner sc; static MyWriter mw; static String filePath="d:\\lmo.in"; public static void main(String[] args) throws IOException { sc = new MyScanner(System.in); mw = new MyWriter(System.out); //while(sc.canRead()) SolveSingleProblem(); mw.close(); } static int piles[]; static long cumul[]; public static void SolveSingleProblem() throws IOException{ int n = sc.nextInt(); piles = new int[n + 1]; cumul = new long[n + 1]; for(int i = 1; i <= n; i++){ piles[i] = sc.nextInt(); cumul[i] = cumul[i - 1] + piles[i]; } int m = sc.nextInt(); while(m--> 0){ int qi = sc.nextInt(); int idx = bS(0, cumul.length - 1, qi); mw.println(idx); } } static int bS(int L, int R, int q){ if(L >= R) return L; int mid = (L + R) >> 1; if(cumul[mid] == q) return mid; if(cumul[mid] < q){ return bS(mid + 1, R, q); } if(cumul[mid] > q){ return bS(L, mid, q); } return -1; } } class MyScanner{ BufferedReader br; StringTokenizer stk; public MyScanner(InputStream stream){ this.br = new BufferedReader(new InputStreamReader(stream),72357); this.stk = new StringTokenizer(""); } public MyScanner(FileReader fr){ this.br = new BufferedReader(fr); this.stk = new StringTokenizer(""); } public String next() throws IOException { while(!stk.hasMoreElements()){ String line = br.readLine(); if(line == null) return null; stk = new StringTokenizer(line); } return stk.nextToken(); } public int nextInt()throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public boolean canRead() throws IOException{ return br.ready(); } public int [] nextIntArray(int n) throws IOException{ int arr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = nextInt(); return arr; } public BigInteger nextBigInteger() throws IOException{ return new BigInteger(this.next()); } } class MyWriter{ PrintWriter pw; public MyWriter(OutputStream out){ this.pw = new PrintWriter(out); } public void print(Object obj){ pw.print(obj.toString()); } public void println(Object obj){ pw.println(obj.toString()); } public void printArray(int[] arr){ for(int i=0;i<arr.length;i++){ if(i==0) println(arr[i]); else println(" "+arr[i]); } } public void printVector(Vector<Integer> arr){ for(int i=0;i<arr.size();i++){ if(i==0) print(arr.elementAt(i)); else print(" " + arr.elementAt(i)); } } public void printIntMatrix(int [][] mt){ for(int i=0;i<mt.length;i++){ for(int j=0;j<mt[0].length;j++){ print(mt[i][j]+" "); } println(""); } println(""); } public void close() { pw.close(); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from bisect import bisect_left n=int(input()) a=list(map(int,input().split())) m=int(input()) q=list(map(int,input().split())) label=[] for i in range(len(a)): if i ==0: x=a[0] label.append(x) else: x=(label[i-1]+a[i]) label.append(x) for i in range(len(q)): print(bisect_left(label,q[i])+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int []check = new int[2000000]; int d = 1; for (int i = 0; i < n; i++) { int x = in.nextInt(); for (int j = 0; j < x; j++) { check[d++] = i + 1; } } int q = in.nextInt(); for (int i = 0; i < q; i++) { int x = in.nextInt(); out.print(check[x]+"\n"); } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from bisect import * input() a = [int(x) for x in input().split()] input() q = [int(x) for x in input().split()] for i in range(1, len(a)): a[i] += a[i - 1] for x in q: print(bisect_left(a, x) + 1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.*; import static java.lang.Math.*; import java.util.Arrays; public class Maybethistimeyouwillcomplie { static PrintWriter out = new PrintWriter(System.out); static Scan in = new Scan(); static int bs(int[] a1, int[] a2, int L, int R, int key){ while (L <= R){ int mid = L + ((R - L) >> 1); if (key >= a1[mid] && key <= a2[mid]) return mid + 1; else if (key > a2[mid]) L = mid + 1; else R = mid - 1; } return -1; } public static void main(String[] args) throws IOException { int n = in.nextInt(), a2[] = new int[n], a1[] = new int[n]; for (int i = 0; i < n; i++) a2[i] = in.nextInt(); for (int i = 1; i < n; i++) a2[i] += a2[i - 1]; a1[0] = 1; for (int i = 1; i < n; i++) a1[i] = a2[i - 1] + 1; int m = in.nextInt(); for (int i = 0; i < m; i++) out.println(bs(a1, a2, 0, a1.length, in.nextInt())); out.flush(); } } class Scan { private final java.io.InputStream stream = System.in; private final byte EOF = -1, NL = '\n', D = '-', SPC = ' ', buffer[] = new byte[0xFFFF]; private char cBuff[] = new char[0xFF]; private int $index, $readCount, itr; private void inc() { cBuff = java.util.Arrays.copyOf(cBuff, cBuff.length << 1); } private boolean readLINE() throws IOException { if ($readCount == EOF) return false; for (itr = 0;;) { while ($index < $readCount) if (buffer[$index] != NL) { if (itr == cBuff.length) inc(); cBuff[itr++] = (char)buffer[$index++]; } else { $index++; return true; } $index = 0; $readCount = stream.read(buffer); if ($readCount == EOF) return true; } } private boolean readPRT() throws IOException { if ($readCount == EOF) return false; T: for (;;) { while ($index < $readCount) if (buffer[$index] > SPC) break T; else $index++; $index = 0; $readCount = stream.read(buffer); if ($readCount == EOF) return false; } for (itr = 0;;) { while ($index < $readCount) if (buffer[$index] > SPC) { if (itr == cBuff.length) inc(); cBuff[itr++] = (char)buffer[$index++]; } else return true; $index = 0; $readCount = stream.read(buffer); if ($readCount == EOF) return true; } } public int nextInt() throws IOException { if (!readPRT()) throw new IOException(); else { int v = 0, i = 0; boolean neg; if (cBuff[i] == D) {neg = true; i++;} else neg = false; while (i < itr) v = (v << 3) + (v << 1) + cBuff[i++] - '0'; return neg ? -v : v; } } public long nextLong() throws IOException { if (!readPRT()) throw new IOException(); else { long v = 0; int i = 0; boolean neg; if (cBuff[i] == D) {neg = true; i++;} else neg = false; while (i < itr) v = (v << 3L) + (v << 1L) + cBuff[i++] - '0'; return neg ? -v : v; } } public String next() throws IOException { return readPRT() ? new String(cBuff, 0, itr) : null; } public char[] nextArr() throws IOException { return readPRT() ? java.util.Arrays.copyOf(cBuff, itr) : null; } public String nextLine() throws IOException { return readLINE() ? new String(cBuff, 0, itr) : null; } public char[] nextLineArr() throws IOException { return readLINE() ? java.util.Arrays.copyOf(cBuff, itr) : null; } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect nw=int(input()) w=input().split(' ') wa=[int(w[0])] for i in range(1,nw): wa.append(int(w[i])+int(wa[i-1])) np=int(input()) p=input().split(' ') for i in range(np): print(bisect.bisect_left(wa,int(p[i]))+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int arr2[1000005] = {0}; int main() { int arr1[100005]; int n; cin >> n; for (int i = 0; i < n; i++) cin >> arr1[i]; int x = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < arr1[i]; j++) { arr2[x] = i + 1; x++; } } int m, y; cin >> m; for (int i = 0; i < m; i++) { cin >> y; cout << arr2[y] << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int A[1001000]; int main() { int n; cin >> n; int sum = 0; int tmp; for (int i = 0; i < n; i++) { cin >> tmp; for (int j = sum + 1; j <= sum + tmp; j++) A[j] = i + 1; sum += tmp; } int m, q; cin >> m; for (int i = 0; i < m; i++) { cin >> q; cout << A[q] << endl; } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = input() a = map(int, raw_input().split()) m = input() q = map(int, raw_input().split()) res = [] for i in range(n): res += [i + 1] * a[i] for i in q: print res[i - 1]
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from bisect import bisect_left from itertools import accumulate def find_ge(a, x): i = bisect_left(a, x) if i != len(a): return i n = input() l1=list(accumulate(list(map(int,input().split())))) q=input() l2 = list(map(int, input().split())) for i in l2: print(find_ge(l1,i)+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) a = list(map(int, input().split())) input() queries = list(map(int, input().split())) ans = [] for i in range(n): ans += [i]*a[i] for q in queries: print(ans[q-1]+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Collections; import java.util.Arrays; import java.util.Collections; import java.util.InputMismatchException; public class MapArray { public static void main(String args[]){ InputReader br = new InputReader(System.in); PrintStream ps = new PrintStream(System.out); int n = br.nextInt(); //int a[]=new int[n]; int tmp1=0,tmp2=0,index=1; int ans[]=new int[1000007]; for(int i=0;i<n;i++) { tmp2=tmp1; tmp1=br.nextInt(); //a[i]+=tmp1; for(int j=1;j<=tmp1;j++){ ans[index]=i+1; //System.out.println("hiii i=" +i); index++; } } int q = br.nextInt(); for(int i=0;i<q;i++){ int tmp3=br.nextInt(); ps.println(ans[tmp3]); } } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import sys f = sys.stdin.readline n = int(f().strip()) arr = [int(v) for v in f().strip().split()] pre = [0]*n pre[0] = arr[0] for i in range(1, n): pre[i] += arr[i]+pre[i-1] from bisect import bisect_left m = int(f().strip()) q = [int(v) for v in f().strip().split()] for v in q: i = bisect_left(pre, v) print(i+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
# -*- coding: utf-8 -*- """ Created on Tue Nov 6 16:31:46 2018 @author: Quaint Sun """ n=int(input()) ai=list(map(int,input().split())) for i in range(1,n): ai[i]=ai[i]+ai[i-1] pile=[1]*(ai[-1]) num=2 for i in range(1,n): for j in range(ai[i-1],ai[i]): pile[j]=num num=num+1 m=int(input()) qi=input().split() for k in qi: print(pile[int(k)-1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
def bins(a, key ,n): l=0; r=n-1 while l<=r: mid = (l+r)//2 if a[mid]<key: l = mid+1 else: if mid==0 or a[mid-1]<key: return mid+1 r = mid n = int(input()) a = list(map(int, input().split())) dp = [a[0]] for i in range(1, n):dp.append(dp[-1]+a[i]) input() for q in map(int, input().split()):print(bins(dp, q, n))
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.PriorityQueue; import java.util.Scanner; import java.lang.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); int [] a = new int [1000005]; int sum = 0; for (int i = 0 ; i < n; i++) { int x = sc.nextInt(); for (int j = 0; j < x;j++) { a[sum + j] = i; } sum += x; } int q = sc.nextInt(); for (int i = 0 ; i < q ; i++) System.out.println(a[sc.nextInt() - 1] + 1); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long binSrc(long long arr[], long long n, long long item) { long long mid, lo, hi; lo = 0; hi = n - 1; while (lo <= hi) { mid = lo + ((hi - lo) / 2); if (arr[mid] == item) return mid + 1; if (item < arr[mid]) { if (!mid) return 1; if (arr[mid - 1] < item) return mid + 1; } if (arr[mid] < item) lo = mid + 1; else hi = mid - 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } long long cumSum[n], sz = n; cumSum[0] = arr[0]; for (long long i = 1; i < n; i++) { cumSum[i] = cumSum[i - 1] + arr[i]; } long long m; cin >> m; while (m--) { long long num; cin >> num; cout << binSrc(cumSum, n, num) << "\n"; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 1; int n, x, q; int a[M]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); a[i] += x + a[i - 1]; } scanf("%d", &q); while (q--) { scanf("%d", &x); printf("%d\n", lower_bound(a, a + n, x) - a); } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int a, b = 0, c, z, y, l, r, t; int main() { cin >> a; int A[a]; for (z = 0; z < a; z++) { cin >> b; if (z > 0) { A[z] = A[z - 1] + b; } else { A[z] = b; } } cin >> a; int B[a]; int C[a]; for (z = 0; z < a; z++) { cin >> B[z]; } for (z = 0; z < a; z++) { l = 0; r = (sizeof(A) / sizeof(A[0])); c = (l + r) / 2; int temp1 = -1, temp2 = -2; while (c >= 0 && c <= (sizeof(A) / sizeof(A[0])) && temp2 != temp1) { if (B[z] <= A[c]) { if (B[z] > A[c - 1]) { C[z] = c + 1; break; } else if (c != 0) { temp2 = temp1; temp1 = A[c]; r = c; } else { C[z] = c + 1; break; } } else if (B[z] > A[c]) { temp2 = temp1; temp1 = A[c]; l = c; } c = (l + r) / 2; } } for (z = 0; z < a; z++) { cout << C[z] << "\n"; } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) l = list(map(int,input().split())) m = int(input()) a = (list(map(int,input().split()))) dic = {i:0 for i in a} tp = a a = sorted(a) for i in range(1,n): l[i]+=l[i-1] i,j = 0,0 while i<m: if a[i]<=l[j]: dic[a[i]]=j+1 i+=1 continue j+=1 for val in tp: print(dic[val])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn], l[maxn], r[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[i] = a[i - 1] + x; l[i] = a[i - 1] + 1; r[i] = a[i]; } r[n + 1] = 1e9; l[n + 1] = 1e9; int m; scanf("%d", &m); while (m--) { int h; scanf("%d", &h); int p = lower_bound(l + 1, l + n + 1, h) - l; if (l[p] > h) p--; printf("%d\n", p); } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(raw_input()) a = map(int,raw_input().split()) l=0 d = {} for i in range(n): for j in range(1,a[i]+1): d[j+l]=i+1 l+=a[i] m = int(raw_input()) b = map(int,raw_input().split()) for i in range(m): print (d[b[i]])
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
from sys import stdin, stdout from collections import defaultdict import heapq import bisect def get_int(): return int(stdin.readline().strip()) def get_ints(): return map(int,stdin.readline().strip().split()) def get_array(): return list(map(int,stdin.readline().strip().split())) def get_string(): return stdin.readline().strip() INF=1e10 #for _ in range(int(stdin.readline())): n=get_int() a=get_array() m=get_int() q=get_array() for i in range(1,n): a[i]+=a[i-1] for i in range(m): print(bisect.bisect_left(a,q[i])+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int a[100005], s[1000005]; int main() { int n, i, j, m, q; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } int cou = 1; for (i = 0; i < n; i++) { for (j = 0; j < a[i]; j++) { s[cou++] = i + 1; } } cin >> m; for (i = 0; i < m; i++) { cin >> q; cout << s[q] << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#!/usr/bin/python import sys def read_obj(cls): return cls(sys.stdin.readline().strip()) def read_obj_list(cls): return map(cls, sys.stdin.readline().strip().split()) def solve(): piles = read_obj(int) worms = read_obj_list(int) juicy_number = read_obj(int) juicy = read_obj_list(int) juicy = [(j, i) for i, j in enumerate(juicy)] juicy.sort() index, limit = 1, worms[0] results = [] for juicy_worm, windex in juicy: while limit < juicy_worm: limit += worms[index] index += 1 results.append((windex, index)) results.sort() for _, index in results: print index if __name__ == "__main__": solve()
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect n = int(input()) a = [int(i) for i in input().split()] pref = [0] * (n + 1) for i in range(1, n + 1): pref[i] = pref[i - 1] + a[i - 1] m = int(input()) b = [int(i) for i in input().split()] for i in range(m): now = b[i] index = bisect.bisect_left(pref, now) print(index)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import sys import io import os import bisect total = 0 failed = 0 def debug(a): print(a, file=sys.stderr) def run(test,res): x = io.StringIO() with io.StringIO(test) as sys.stdin: with x as sys.stdout: work() z = x.getvalue().strip() sys.stdout = sys.__stdout__ print("Passed?", z == res) print("Expected: ", res) print("Actual : ", z) global total, failed total += 1 failed += 1 if (z != res) else 0 def work(): n = int(input()) a = list(map(int, input().split())) b = [0]*n for i, e in enumerate(a): b[i] = e + (0 if i==0 else b[i-1]) m = int(input()) q = list(map(int, input().split())) for e in q: print(1+bisect.bisect_left(b, e)) def test(): run("""5 2 7 3 4 9 3 1 25 11""", """1 5 3""") if('LOCALTEST' in os.environ): test() print("\n Result: %s (%d total, %d failed)" % ("FAILED" if (failed>0) else "PASSED", total, failed)) else: work()
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) a = [None for i in range(1000000)] j = 0 h = 1 for i in list(map(int, input().split())): for _ in range(i): a[j] = h j+=1 h+=1 input() for i in list(map(int, input().split())): print(a[i-1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.util.*; import java.io.*; public class EjerH3{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, m, cont; n=sc.nextInt(); int[] arr = new int[n+1]; cont=0; arr[0]=0; int res=0; for (int i = 1; i < n+1; i++) { cont=sc.nextInt(); arr[i]= arr[i-1]+cont; } m=sc.nextInt(); int[] arr2 = new int[m]; for (int i = 0; i < m; i++) { res=Arrays.binarySearch(arr, 1, n+1, sc.nextInt()); if (res<0) { res=res*-1 -1; System.out.println(res); } else { System.out.println(res); } } } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int A[100005]; int bsearch(int q, int l, int r) { if (l == r - 1) { return l; } int mid = (l + r) / 2; if (A[mid - 1] < q && q <= A[mid]) { return mid; } else if (q <= A[mid - 1]) { return bsearch(q, l, mid); } else { return bsearch(q, mid, r); } return -1; } int main() { int N; cin >> N; A[0] = 0; for (int i = 1; i <= N; ++i) { cin >> A[i]; A[i] += A[i - 1]; } int M, query; cin >> M; for (int i = 0; i < M; ++i) { cin >> query; cout << bsearch(query, 0, N + 1) << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
first = input() n = (int)(first) second = input().split() piles = [(int)(x) for x in second] first = input() m = (int)(first) second = input().split() juicy = [(int)(x) for x in second] for i in range(1,n): piles[i] = piles[i]+piles[i-1] for i in range(m): beg = 0 last = n-1 while(beg<=last): mid = (int)((beg+last)/2) if(juicy[i] > piles[mid]): beg = mid+1 elif(juicy[i] < piles[mid] and (mid-1)>=0 and juicy[i] <= piles[mid-1]): last = mid-1 else: print(mid+1) break
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; int p[100010]; p[0] = 0; for (i = 0; i < n; i++) { int b; cin >> b; p[i + 1] = p[i] + b; } int m; cin >> m; for (i = 0; i < m; i++) { int worm; cin >> worm; int ans = lower_bound(p, p + n, worm) - p; cout << ans << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
def busca_binaria(lista, busca): inicio, fim = 0, len(lista)-1 while inicio <= fim: meio = (inicio + fim)/2 if busca == lista[meio]: return meio + 1 else: if lista[meio] < busca: inicio = meio + 1 else: fim = meio - 1 return inicio + 1 n = int(raw_input()) a = raw_input().split() m = int(raw_input()) q = raw_input().split() q = map(int, q) soma = 0 l = [] for i in a: soma += int(i) l.append(soma) for j in range(len(q)): print busca_binaria(l,q[j])
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; long long ara[100010]; vector<long long> v1; long long val, sum, val1; long long f, l, mid, mid1, res1, res2, s; long long n, m; void bs(long long s) { f = 0; l = n - 1; while (f <= l) { mid = (f + l) / 2; mid1 = mid - 1; res1 = ara[mid]; res2 = ara[mid1]; if (s < res1 && mid1 < 0) { cout << "1"; break; } else if (s < res1 && s < res2) l = mid - 1; else if (s > res1 && s > res2) f = mid + 1; else if ((s >= res1 && s >= res2) || (s < res1 && s > res2)) { cout << mid + 1; break; } else l = mid - 1; } } int main() { sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> val; sum += val; ara[i] = sum; } cin >> m; for (int i = 0; i < m; i++) { cin >> val1; v1.push_back(val1); } for (int i = 0; i < m; i++) { s = v1.at(i); bs(s); cout << endl; } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = raw_input() numberOfWorms = map(int, raw_input().split()) m = input() juicyWorms = map(int, raw_input().split()) for i in range(1,len(numberOfWorms)): numberOfWorms[i] += numberOfWorms[i-1] for i in juicyWorms: start = 0 end = len(numberOfWorms) - 1 while start <= end: mid = (end - start) / 2 + start if numberOfWorms[mid] < i: start = mid + 1 else: end = mid - 1 print start + 1
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
R = lambda: map(int, raw_input().split()) n, a, m, q = input(), R(), input(), R() for i in range(1,n): a[i] += a[i-1] for x in q: lo, hi = 0, n while lo < hi: mid = (lo + hi) / 2 if a[mid] < x: lo = mid + 1 elif a[mid] >= x: hi = mid print lo + 1
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.util.*; public class bit8 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; a[0] = in.nextInt(); for(int i=1;i<n;i++){ a[i] = in.nextInt()+a[i-1]; } int m = in.nextInt(); int b; for(int i=0;i<m;i++){ b = Arrays.binarySearch(a, in.nextInt()); if(b>=0) System.out.println(b+1); else{ System.out.println(-b); } } } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int binary(long long int s[], int t, int m) { int l = 0, h = m - 1; while (l <= h) { int mid = (l + h) / 2; if (s[mid] == t) return mid; else if (l == h) return l; else if (t > s[mid]) l = mid + 1; else if (t < s[mid]) h = mid; } } int main() { int n; cin >> n; long long int sum[n + 1]; sum[0] = 0; for (int i = 1; i <= n; i++) { int temp; cin >> temp; sum[i] = sum[i - 1] + temp; } int m; cin >> m; for (int i = 0; i < m; i++) { int t1; cin >> t1; long long int y = binary(sum, t1, n + 1); cout << y << endl; } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
L= [] input() r = 1 for k in input().split(): L += [r] * int(k) r = r + 1 input() for j in input().split(): print(L[int(j) - 1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import sys def solve(): n , = read() mem = read() count = sum(mem) mem2 = [0]*count index = 0; for i in range(n): temp = mem[i]; for j in range(temp): mem2[index] = i index+=1 m , = read() second = read() for i in range(m): temp = second[i] print(mem2[temp-1]+1) def read(mode=2): inputs = input().strip() if mode == 0: return inputs # String if mode == 1: return inputs.split() # List of strings if mode == 2: return list(map(int, inputs.split())) # List of integers def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) if isinstance(s, tuple): s = " ".join(map(str, s)) s = str(s) print(s, end="") def run(): if sys.hexversion == 50594544 : sys.stdin = open("test.txt") res = solve() write(res) run()
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.util.*; public class bs { public static void main(String[] args){ // int[] arr={1,2,3,4}; Scanner scn=new Scanner(System.in); // long t=scn.nextLong(); // while(t-->0){ int n=scn.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++){a[i]=scn.nextInt(); if(i>0){a[i]+=a[i-1];}} int m=scn.nextInt(); int[] b=new int[m]; for(int i=0;i<m;i++){b[i]=scn.nextInt();} for(int i=0;i<m;i++){ int targ=b[i]; int lo=0,hi=n-1; int ans=0; while(lo<=hi){ int mid=lo+(hi-lo)/2; if(a[mid]==targ){ans=mid+1; break;} if(a[mid]<targ){ lo=mid+1; }else{ ans=mid+1; hi=mid-1; } } System.out.println(ans); } // } } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, i, m, j, cnt = 0, ans, x, sum = 0; cin >> n; int a[n], c[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; c[i] = sum; } cin >> m; int b[m]; for (i = 0; i < m; i++) cin >> b[i]; for (j = 0; j < m; j++) { cout << lower_bound(c, c + n, b[j]) - c + 1 << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.*; import java.util.*; public class Worms { static StringTokenizer st; static BufferedReader in; static PrintWriter out; private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static byte nextByte() throws IOException { return Byte.parseByte(next()); } private static String next() throws IOException { while(st == null || ! st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public static int BS(int size,int Val,int F[]) { int Start = 0; int End = size; while (Start < End) { int mid = (End + Start) / 2; if (Val <= F[mid]) { End = mid; } else { Start = mid + 1; } } return Start+1; } public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n=nextInt(); int Worms[]=new int[n]; for (int i = 0; i < n; i++) { Worms[i]=nextInt(); if (i>0) { Worms[i]+=Worms[i-1]; } } int m=nextInt(); for (int j = 0; j < m; j++) { int Val=nextInt(); out.println(BS(n,Val,Worms)); } out.close(); // for (int i = 0,index=0; i < n; i++) // { // for (int j = 0; j < Worms[i]; j++) // { // A[index]=i+1; // index++; // } // } // System.out.println(Arrays.toString(A)); // int m=nextInt(); // for (int i = 0; i < m; i++) // { // System.out.println(A[nextInt()-1]); // } // } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x; vector<int> sum(n); for (int i = 0; i < n; i++) { cin >> x; if (i == 0) sum[i] = x; else { sum[i] = x + sum[i - 1]; } } int m; cin >> m; vector<int> j(m); for (int i = 0; i < m; i++) cin >> j[i]; for (int i = 0; i < m; i++) { x = j[i]; int l = 0, r = n - 1; if (sum[0] >= x) { cout << 1 << endl; continue; } while (r - l > 1) { int mid = (l + r) / 2; if (sum[mid] >= x) r = mid; else l = mid; } cout << r + 1 << endl; } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; long long int M, k, Q, sum = 0, a = 1, W; vector<int> x; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &W); for (int j = 0; j < W; j++) { x.push_back(i); } } scanf("%lld", &M); for (k = 1; k <= M; k++) { scanf("%lld", &Q); printf("%d\n", x[Q - 1]); } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect n = int(input()) lst = [i for i in map(int, input().split())] for i in range(1, len(lst)): lst[i] += lst[i-1] _ = input() for item in map(int, input().split()): print(bisect.bisect_left(lst, item) + 1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect int(input()) A = [0] for i in map(int, input().split()): A.append(A[-1]+i) int(input()) for i in map(int, input().split()): print(bisect.bisect_left(A, i))
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.util.Arrays; import java.util.Scanner; public class Demo { /** * @param args */ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] a=new int[n]; a[0]=in.nextInt(); for(int i=1;i<n;i++){ a[i]=in.nextInt()+a[i-1]; } int m=in.nextInt(); int q; int k=0; StringBuilder s = new StringBuilder(); for(int i=0;i<m;i++){ q=in.nextInt(); k=Arrays.binarySearch(a, q); if(k<0){k++; k=0-k;} System.out.println(k+1); /*while(k<n){ if(q<=a[k]){ s.append((k+1)+"\n"); break; } k++; }*/ //k=0; } in.close(); System.out.println(s.toString()); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import sys def binarySearch(arr, val): l = 0 r = len(arr) while(l < r - 1): m = int((l + r) / 2) if(arr[m] < val): l = m else: r = m return r n = int(sys.stdin.readline()) z = [int(x) for x in (sys.stdin.readline()).split()] an = [] for i in range(n): k = z[i] while(k > 0): an.append(i + 1) k -= 1 m = int(sys.stdin.readline()) qm = [int(x) for x in (sys.stdin.readline()).split()] for i in qm: print(an[i - 1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class aaa { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws NumberFormatException, IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = nextInt(); int a[] = new int[n]; a[0] = nextInt(); for (int i = 1; i < n; i++) a[i] = nextInt() + a[i - 1]; int m = nextInt(); while (m-- > 0) { int ans = Arrays.binarySearch(a, nextInt()); pw.println(ans < 0 ? Math.abs(ans) : ans + 1); } pw.close(); } private static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } private static String next() throws IOException { while(st==null||!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
n = int(input()) a = [0] a.extend(list(map(int,input().split()))) m = int(input()) q = list(map(int,input().split())) t = a[0] temp = [t] for i in range(1,len(a)): t+=a[i] temp.append(t) t = [0]*(sum(a)+1) x = 1 for i in range(1,n+1): for j in range(temp[i-1],temp[i]+1): t[j]=x x+=1 for item in q: print(t[item-1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
""" // Author : snape_here - Susanta Mukherjee """ from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def fi(): return float(input()) def si(): return input() def mi(): return map(int,input().split()) def li(): return list(mi()) def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def isPrime(n): if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def gcd(x, y): while y: x, y = y, x % y return x mod=1000000007 from math import log,sqrt,factorial,cos,tan,sin,radians,ceil,floor import bisect from decimal import * getcontext().prec = 25 abc="abcdefghijklmnopqrstuvwxyz" pi=3.141592653589793238 def fn(n): c=0 while n%2==0: c+=1 n//=2 return c def main(): n=ii() a=li() p=[] s=0 for i in range(n): s+=a[i] p.append(s) m=ii() q=li() for i in range(m): c=bisect.bisect_left(p,q[i]) print(c+1) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main()
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int m; cin >> m; for (int i = 1; i < n; i++) { v[i] += v[i - 1]; } for (int i = 1; i <= m; i++) { int target; cin >> target; int start = 0; int end = n - 1; while (start <= end) { int mid = (start + end) / 2; if (v[mid] >= target && target > v[mid - 1] && mid != 0) { cout << mid + 1 << endl; break; } else if (v[mid] >= target && mid == 0) { cout << 1 << endl; break; } else if (v[mid] < target) { start = mid + 1; } else { end = mid - 1; } } } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; int A[n]; vector<int> V1; for (int i = 0; i < n; i++) { cin >> A[i]; for (int j = 0; j < A[i]; j++) V1.push_back(i + 1); } cin >> m; for (int i = 0; i < m; i++) { int w; cin >> w; cout << V1[w - 1] << endl; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.*; import java.math.*; import java.security.KeyStore.Entry; import java.util.*; public class TestClass { private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static void soln() { int n = nI(); int arr[] = new int[n+1]; for(int i=0;i<n;i++) arr[i+1] = arr[i]+nI(); int m = nI(); while(m-->0){ int p = nI(); int l = 1,r = n,ans = 1; while(l<=r){ int mid = (l+r)>>1; if(arr[mid]>=p){ r = mid-1; ans = mid; } else l = mid+1; } pw.println(ans); } } public static void main(String[] args) { InputReader(System.in); pw = new PrintWriter(System.out); soln(); pw.close(); } // To Get Input // Some Buffer Methods public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private static int nI() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nL() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nLi() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Ruff4 { static PrintWriter out =new PrintWriter(System.out); public static void main(String[] args) { Scanner vicky=new Scanner(System.in); int n=vicky.nextInt(); int [] ar =new int [n]; ar[0]=vicky.nextInt(); for(int i=1;i<n;i++) { ar[i]=vicky.nextInt(); ar[i]+=ar[i-1]; } int m=vicky.nextInt(); while(m-->0) { int x=vicky.nextInt(); int l=Arrays.binarySearch(ar, x); if(l>=0) out.println(l+1); else out.println(-l); } out.close(); } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
N = int(input()) L = list(map(int,input().split())) J = int(input()) L1 = list(map(int,input().split())) NewL = [0]*sum(L) temp = 0 for i in range(N): for j in range(L[i]): NewL[temp+j]=i+1 temp+=L[i] for i in L1: print(NewL[i-1])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.lang.Math; import java.io.*; public class Worm { public static void main(String args[]) throws IOException { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); int [] ar=new int [1000001]; int n=Integer.parseInt(br.readLine()); int pos=1; String s = br.readLine(); String [] parts =s.split(" "); for(int i=0;i<n;i++) { int x=Integer.parseInt(parts[i]); for(int j=1;j<=x;j++) { ar[pos++]=i+1; } } int m=Integer.parseInt(br.readLine()); String str=br.readLine(); String [] par =str.split(" "); for(int i=0;i<m;i++) { // int y=Integer.parseInt(par[i]) System.out.println(ar[Integer.parseInt(par[i])]); } } }
JAVA
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long int n; cin >> n; unordered_map<long long int, long long int> pos; long long int prev, now; cin >> now; for (long long int i = 1; i <= now; ++i) pos[i] = 1; for (long long int i = 2; i <= n; ++i) { prev = now + 1; long long int a; cin >> a; now += a; for (long long int j = prev; j <= now; ++j) pos[j] = i; } long long int m; cin >> m; while (m--) { long long int a; cin >> a; cout << pos[a] << '\n'; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
N, a, M, q = int(raw_input()), map(int, raw_input().split()), int(raw_input()), map(int, raw_input().split()) for i in range(1, N): a[i] += a[i - 1] for i in range(M): lo, hi = 0, N while(lo < hi): m = (lo + hi) / 2 if(a[m] < q[i]): lo = m + 1 else: hi = m print lo + 1
PYTHON
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
import bisect arrSize=int(input()) arr=list(map(int,input().split())) sum=0 prefix_array=[] for i in arr: sum+=i prefix_array.append(sum) quries=int(input()) que=list(map(int,input().split())) for i in que: print(bisect.bisect_right(prefix_array,i-1)+1)
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int dp[1000000]; int main() { int n; cin >> n; int arr[n]; int sum = 0; int count = 1; int k; for (int i = 0; i < n; i++) cin >> k, sum += k, dp[sum] = count++; count = 1; for (int i = 1; i <= sum; i++) { if (dp[i]) { count++; } else dp[i] = count; } int q; cin >> q; while (q--) { int a; cin >> a; cout << dp[a] << "\n"; } return 0; }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
# def bsearch(arr, target): # lo = 0 # hi = len(arr) - 1 # while lo <= hi: # mi = (lo + hi) // 2 # if arr[mi] == target: # return mi # elif arr[mi] > target: # hi = mi - 1 # else: # lo = mi + 1 # return (lo + hi) // 2 + 1 # if __name__ == '__main__': # n = int(input()) # piles = [int(x) for x in input().split()] # w = int(input()) # worms = [int(x) for x in input().split()] # for i in range(len(piles) - 1): # piles[i + 1] += piles[i] # for worm in worms: # print(bsearch(piles, worm) + 1) if __name__ == '__main__': n = int(input()) piles = [int(x) for x in input().split()] w = int(input()) worms = [int(x) for x in input().split()] # for i in range(len(piles) - 1): # piles[i + 1] += piles[i] d = {} w = 1 for i in range(len(piles)): # print(piles[i]) for j in range(piles[i]): d[w] = i w += 1 for w in worms: print(d[w] + 1) # def bsearch(arr, lo, hi, target): # while lo <= hi: # mi = (lo + hi) // 2 # if arr[mi] == target: # return mi # elif arr[mi] > target: # hi = mi - 1 # else: # lo = mi + 1 # return (lo + hi) // 2 + 1 # if __name__ == '__main__': # n = int(input()) # piles = [int(x) for x in input().split()] # w = int(input()) # worms = [int(x) for x in input().split()] # for i in range(len(piles) - 1): # piles[i + 1] += piles[i] # sworms = list(zip(worms, [i for i in range(w)])) # sworms.sort() # # print(sworms) # res = [None] * w # # j = bsearch(piles, 0, n, worms[0]) + 1 # j = 0 # i = 0 # while i < w: # u, v = sworms[i] # res[v] = bsearch(piles, j, n, u) + 1 # i += 1 # for v in range(w): # print(res[v])
PYTHON3
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, s = 0; vector<int> result; for (int i = 1; i <= n; i++) { cin >> a; s += a; result.push_back(s); } int m, b; cin >> m; for (int i = 1; i <= m; i++) { cin >> b; int answer = lower_bound(result.begin(), result.end(), b) - result.begin(); cout << answer + 1 << "\n"; } }
CPP
474_B. Worms
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer n (1 ≤ n ≤ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103, a1 + a2 + ... + an ≤ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≤ m ≤ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≤ qi ≤ a1 + a2 + ... + an), the labels of the juicy worms. Output Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is. Examples Input 5 2 7 3 4 9 3 1 25 11 Output 1 5 3 Note For the sample input: * The worms with labels from [1, 2] are in the first pile. * The worms with labels from [3, 9] are in the second pile. * The worms with labels from [10, 12] are in the third pile. * The worms with labels from [13, 16] are in the fourth pile. * The worms with labels from [17, 25] are in the fifth pile.
2
8
#include <bits/stdc++.h> using namespace std; long long binarySearch(vector<long long> &A, int i, int j, long long n) { int m, result = -1; while (i <= j) { m = (i + j) >> 1; if (A[m] == n) { result = m; break; } else { if (n > A[m]) i = m + 1; else j = m - 1; } } if (result == -1) result = (-1) * i - 1; return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, sq, out; cin >> n; vector<long long> vec(n); for (int i = 0; i < vec.size(); i++) { cin >> vec[i]; if (i) vec[i] += vec[i - 1]; } cin >> m; while (m--) { cin >> sq; out = binarySearch(vec, 0, vec.size() - 1, sq); if (out < 0) { out *= -1; out--; } cout << out + 1 << endl; } return 0; }
CPP