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
#include <bits/stdc++.h> using namespace std; int a[10000001], n, m, x; int main() { cin >> n; int beg = 1; int prev = 0; for (int i = 0; i < n; i++) { cin >> x; for (int j = beg; j <= prev + x; j++) a[j] = i + 1; prev += x; beg += x; } cin >> m; for (int i = 0; i < m; i++) { cin >> x; cout << a[x] << 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; const unsigned int x = 1000000007; int n[5] = {0, 1, 2}; map<int, int> m; int cnt = 0; int a, b; int z, y; int low, high, mid; void solve(int y) { while (low <= high) { mid = (high + low) / 2; if (m[mid] >= y && m[mid - 1] < y) return; else if (m[mid] < y) low = mid + 1; else if (m[mid] > y && m[mid - 1] >= y) high = mid - 1; } return; } int main() { cin >> a; for (int i = 1; i <= a; i++) { cin >> b; m[i] = b + m[i - 1]; } cin >> z; for (int j = 1; j <= z; j++) { low = 1, high = a; cin >> y; solve(y); cout << mid << 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.StringTokenizer; public class B474 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); StringTokenizer s=new StringTokenizer(br.readLine()); int arr[]=new int[n]; for (int i=0;i<n;i++) { int val=Integer.parseInt(s.nextToken()); if (i==0) arr[0]=val; else arr[i]=arr[i-1]+val; } int m=Integer.parseInt(br.readLine()); s=new StringTokenizer(br.readLine()); int ans[]=new int[m]; for (int i=0;i<m;i++) { int k=Integer.parseInt(s.nextToken()); int an= binarySearch(arr,0,arr.length-1,k); ans[i]=an+1; } //print ans for (int i: ans) { System.out.println(i); } } static int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); } else return l; } }
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
def func(query,low,high): mid = (low+high)//2 if query <= num_rooms[mid] and query > num_rooms[mid-1]: return mid elif query > num_rooms[mid] and query <= num_rooms[mid+1]: return mid+1 elif query < num_rooms[mid]: return func(query,low,mid) elif query > num_rooms[mid]: return func(query,mid,high) n = int(input()) num_rooms = list(map(int, input().split())) num_rooms.insert(0,0) for i in range(n+1): if i>0: num_rooms[i] += num_rooms[i-1] q = int(input()) query_store = list(map(int, input().split())) ans_store = [] for query in query_store: ans_store.append(func(query,1,n)) for m in range(q): print(ans_store[m])
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()) def binarySearch(x,arr,start,end): if start >= end: return end mid = start + (end - start)//2 if arr[mid] == x: return mid elif arr[mid] < x: return binarySearch(x,arr,mid+1,end) elif arr[mid] > x: return binarySearch(x,arr,start,mid) t=list(map(int,input().split())) csum=[t[0]] #2 values for i in range(1,n): csum.append(t[i]+csum[i-1]) a=int(input()) j=list(map(int,input().split())) for i in range(a): print(binarySearch(j[i],csum,0,n)+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.math.*; import java.io.*; public class Solution { static Random random = new Random(); public static void main(String[] args) throws IOException{ FastScanner scanner = new FastScanner(System.in); int n = scanner.nextInt(); int sum = 0; int[] arr = new int[n+1]; for(int i = 1; i <= n; i++) { sum += scanner.nextInt(); arr[i] = sum; } int m = scanner.nextInt(); for(int i = 0; i < m; i++) { int q = scanner.nextInt(); int res = binarySearch(arr, q, 0, arr.length); System.out.println(res); } } static int binarySearch(int[] arr, int k, int low, int high) { if(low > high) { return -1; } int mid = low + (high-low)/2; if(mid != 0 && k <= arr[mid] && k >= arr[mid-1] + 1) { return mid; } else if(k > arr[mid]) { return binarySearch(arr, k, mid + 1, high); } else { return binarySearch(arr, k, low, mid-1); } } static void printArr(int[] arr) { for(int k: arr) { System.out.print(k + " "); } System.out.println(); } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch(Exception e) { e.printStackTrace(); } } boolean hasNextToken() { if(st == null) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { } } return st.hasMoreTokens(); } String next() { while(st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() throws IOException{ return br.readLine(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(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
L=[] r=1 input() for i in input().split(): L+=[r]*int(i) r+=1 input() for i in input().split(): print(L[int(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())) m=int(input()) q=list(map(int,input().split())) b=[] for i in range(n): b+=[i+1]*a[i] 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
import java.util.*; public class Main{ static int binSearch(int[] arr,int low,int high,int x){ if(low<=high){ int mid=low+(high-low)/2; // System.out.println(mid); if((mid>low && arr[mid]>=x && arr[mid-1]<x) || (mid==low && arr[mid]>=x)) return mid; else if(arr[mid]<x ) return binSearch(arr,mid+1,high,x); else return binSearch(arr,low,mid-1,x); } else return -1; } public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ int x=in.nextInt(); if(i==0) arr[0]=x; else arr[i]=arr[i-1]+x; } int m=in.nextInt(); for(int i=0;i<m;i++){ int w=in.nextInt(); System.out.println(binSearch(arr,0,n-1,w)+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() { long long n, m; cin >> n; long long partialSum[n]; long long sum = 0; long long x = 0; while (x < n) { long long wormQty; cin >> wormQty; sum += wormQty; partialSum[x] = sum; x++; } cin >> m; long long labelArray[m]; for (long long i = 0; i < m; i++) { long long label; cin >> label; labelArray[i] = label; } for (long long i = 0; i < m; i++) { long long label = labelArray[i]; long long l = -1; long long r = n; while (l + 1 < r) { int mid = l + (r - l) / 2; if (partialSum[mid] < label) { l = mid; } else { r = mid; } } cout << r + 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
#include <bits/stdc++.h> using namespace std; long long n, i, x, q, query, pos; vector<long long> t; vector<long long>::iterator iter; int main() { ios::sync_with_stdio(false); cin >> n; t.push_back(0); for (i = 1; i <= n; i++) { cin >> x; t.push_back(x + t[i - 1]); } cin >> q; for (i = 0; i < q; i++) { cin >> query; iter = lower_bound(t.begin(), t.end(), query); pos = iter - t.begin(); cout << pos << 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 bisect def ni(): return int(raw_input()) def nis(): return map(int, raw_input().split()) def si(): return raw_input() n = ni() a = nis() m = ni() q = nis() for i in range(n - 1): a[i + 1] += a[i] ans = [] for x in q: ans.append(str(bisect.bisect_left(a, x) + 1)) print '\n'.join(ans)
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
n = int(input()) a = [int(i) for i in input().split()[:n]] m = int(input()) q = [int(j) for j in input().split()[:m]] v = []*n w = [] z1 = [] x1=0 x2=0 for k in range(n): s1 = 1+x1 s2 = a[k]+x2 x1 = s2 x2 = x2+a[k] v.append([s1,s2]) z1.append(s1) # print(v) def binary_search(arr, x): low = 0 high = len(arr) - 1 mid = 0 while low <= high: mid = (high + low) // 2 if arr[mid] < x: low = mid + 1 elif arr[mid] > x: high = mid - 1 else: return mid return high # res = binary_search(z1, 1) # print(res) for d in range(m): res = binary_search(z1, q[d]) print(res+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= [int(i) for i in input().split()] m= int(input()) q= [int(i) for i in input().split()] b= q.copy() b.sort() c={} i, j= 0, 0 w=0 while i<len(q) and j<len(a): if w< b[i]<= w+a[j]: c[b[i]]= j+1 i+=1 elif b[i] > w+a[j]: w+= a[j] j+=1 for i in q: print(c[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
def GetWorms(worms): w = [1] s = worms[0] for i in range(1,len(worms)): w.append(s+1) s += worms[i] return w def Solve(worms,label,maxx): low = 0 high = len(worms)-1 while low <= high: mid = (low+high)//2 if label > worms[mid]: low = mid+1 elif label < worms[mid]: if mid > 0: if label > worms[mid-1]: mid -= 1 break high = mid-1 else: break return mid+1 def main(): n = int(input()) worms = list(map(int,input().split())) m = int(input()) labels = list(map(int,input().split())) s = sum(worms) worms = GetWorms(worms) #print (worms) for i in labels: print (Solve(worms,i,s)) 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
import java.io.*; import java.util.Arrays; import java.util.Locale; import java.util.StringTokenizer; public class Main implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new Main(), "", 128 * (1L << 20)).start(); } void init() throws FileNotFoundException { Locale.setDefault(Locale.US); if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } public void run() { try { timeBegin = System.currentTimeMillis(); init(); solve(); out.close(); time(); } catch (Exception e) { e.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } String readString(String s) throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), s + "\n \t"); } catch (Exception e) { return null; } } return tok.nextToken(); } double readDouble() throws IOException { return Double.parseDouble(readString()); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } int readInt(String s) throws IOException { return Integer.parseInt(readString(s)); } boolean isPrime(int a) throws IOException { int del=0; for(int i=2;i<Math.sqrt(a)+2;i++){ if(a%i==0){ del=i; break; } } if(del>0){ return false; } else return true; } void solve() throws IOException { int n = readInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = readInt(); int m = readInt(); int[] b = new int[m]; for(int i=0;i<m;i++) b[i] = readInt(); int[] sum = new int[n]; sum[0] = a[0]; for(int i=1;i<n;i++){ sum[i] = a[i]+sum[i-1]; } int r=0; for(int i=0;i<m;i++){ r=Math.abs(Arrays.binarySearch(sum, b[i])); if(r<sum.length && sum[r]==b[i])r++; out.println(r); } } }
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.util.StringTokenizer; import java.util.TreeMap; public class wormsTreeMap { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int []piles = new int[n]; piles[0]=Integer.parseInt(st.nextToken()); int sum=0; for(int i=1; i<n; i++) { piles[i]=Integer.parseInt(st.nextToken())+piles[i-1]; } int m = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); int []juicy = new int[m]; for(int i=0; i<m; i++) { juicy[i]=Integer.parseInt(st.nextToken()); } TreeMap<Integer, Integer> tree = new TreeMap<Integer, Integer>(); int count =1; for(int i=0; i<n; i++) { while(count<=piles[i]) { tree.put(count, i); count++; } } for(int i=0; i<m; i++) { int pos= tree.get(juicy[i]); out.println(pos+1); } out.close(); out.flush(); } }
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
R=lambda:map(int,raw_input().split()) n,a,m,b=input(),R(),input(),R() index,s=[0]*(sum(a)+1),1 for i in range(n): for j in range(a[i]): index[s+j]=i+1 s+=a[i] print '\n'.join(map(str,[index[x] for x in b]))
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
n = int(input()) lin = list(map(int, input().split())) mw = {} pile = 1 wty = 0 for i in lin: j = i while j: wty += 1 mw[wty] = pile j -= 1 pile += 1 m = int(input()) lim = list(map(int, input().split())) for i in lim: print(mw[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 os import sys from io import BytesIO, IOBase from itertools import accumulate from bisect import bisect_left def solution(arr, brr, n, m): arr = list(accumulate(arr)) for q in brr: write(bisect_left(arr, q) + 1) def main(): n = r_int() arr = r_array() m = r_int() brr = r_array() solution(arr, brr, n, m) 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) def input(): return sys.stdin.readline().rstrip("\r\n") def write(*args, end='\n'): for x in args: sys.stdout.write(str(x) + ' ') sys.stdout.write(end) def r_array(): return [int(x) for x in input().split()] def r_int(): return int(input()) 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
import sys stdin=sys.stdin def sol1(): n=int(stdin.readline().strip()) a=map(int,stdin.readline().strip().split()) s=[0]*(10**6+1) curr=0 for i in range(n): for j in range(a[i]): s[curr]=i curr+=1 m=int(stdin.readline()) for q in map(int,stdin.readline().strip().split()): print s[q-1]+1 def main(): sol1() 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
n=int(input()) a=list(map(int,input().split())) m=int(input()) queries=list(map(int, input().split())) ans = [] for i in range(n): ans+=[i]*a[i] #print(ans) 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
n = int(input()) ai = input().split() m = int(input()) qi = input().split() ai[0] = int(ai[0]) for i in range(1, n): ai[i] = int(ai[i]) + int(ai [i - 1]) for i in range(m): qi[i] = int(qi[i]) left = 0 right = n while right > left: mid = (right + left) // 2 if qi[i] < ai[mid]: right = mid elif qi[i] > ai[mid]: left = mid + 1 else: print(mid + 1) break else: if qi[i] < ai[left]: print(left + 1) else: print(right + 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 binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] <= key): count = mid+1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count def main(): n = int(input()) a = list(map(int, input().split())) m = int(input()) q = list(map(int, input().split())) d = [] c = 0 for i in range(n): d.append(c + 1) c = c + a[i] d.append(c) # print(d) for i in range(m): if q[i] == d[len(d) - 1]: print(len(d) // 2) else: print((binarySearchCount(d,len(d),q[i])+1)//2) 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().rsplit())) m = int(input()) q = list(map(int, input().rsplit())) b = [] a0 = 0 for i in range(n): j = a0 + 1 while j <= a[i] + a0: b.append(i + 1) j += 1 a0 = a[i] for j in range(m): print(b[q[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
function sum(array){ var result = 0; for(var i = 0; i < array.length; i++){ result += +array[i]; } return result; } var n = +readline(), a = readline().split(" ").map(Number), m = +readline(), q = readline().split(" ").map(Number); var array = [], all = sum(a), cnt = a[0]; for(var i = 1, j = 1; i <= all; i++){ if(i <= cnt){ array[i] = j; } else{ cnt += a[j]; j++; array[i] = j; } } for(i = 0; i < m; i++){ print(array[q[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
l=[] n=input() a=map(int,raw_input().split(' ')) for i in range(n): l+=[i+1]*a[i] m=input() q=map(int,raw_input().split(' ')) for i in range(m): print l[q[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
#include <bits/stdc++.h> using namespace std; clock_t start; mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const long long N = 1e6 + 10; long long pile[N]; void solve() { long long n; cin >> n; long long a, curr = 1, sum = 0; for (long long i = 0; i < n; i++) { cin >> a; sum += a; for (long long j = curr; j <= sum; j++) pile[j] = i + 1; curr = sum + 1; } long long m; cin >> m; for (long long i = 0; i < m; i++) { long long q; cin >> q; cout << pile[q] << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; start = clock(); cout << fixed << setprecision(15); while (t--) { solve(); } double time_taken = double(clock() - start) / double(CLOCKS_PER_SEC); cerr << time_taken; }
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 search(a, x): l, r = 0, len(a) - 1 while l <= r: mid = (l + r) // 2 if a[mid] >= x and a[mid - 1] < x: return mid elif a[mid] < x: l = mid + 1 else: r = mid - 1 return 0 n = int(input()) a = list(map(int, input().split())) for i in range(1, n): a[i] += a[i - 1] q = int(input()) qu = list(map(int, input().split())) for x in qu: print(search(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.util.Scanner; public class Worms { private static int arr[]; public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); arr = new int[n]; arr[0] = sc.nextInt(); for (int i = 1; i < n; i++) arr[i] = arr[i - 1] + sc.nextInt(); int m = sc.nextInt(); for (int i = 0; i < m; i++){ int f = sc.nextInt(); if(f<arr[0]){ System.out.println(1); } else{ findPile(0, n-1, f); } } } private static void findPile(int si, int ei, int f) { int mid = si + ((ei - si) / 2); //System.out.println("si="+si+" ei="+ei+" mid="+mid+" arr[mid]="+arr[mid]+" f="+f); if (arr[mid] >= f && arr[mid - 1] <= f) { if(arr[mid-1] == f) System.out.println((mid)); else System.out.println((mid+1)); return; } else if (arr[mid] > f) findPile(si, mid, f); else if (arr[mid] < f) findPile(mid + 1, ei, f); } }
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
def func(arr,x): i=0 j=len(arr)-1 while i<=j: mid=(i+j)//2 if mid==0: if x>=1 and x<=arr[mid]: return mid+1 else: return mid+2 if x<=arr[mid] and x>=arr[mid-1]+1: return mid+1 elif x>arr[mid]: i=mid+1 elif x<arr[mid-1]+1: j=mid-1 n=int(input()) a=[int(x) for x in input().split()] k=int(input()) labels=[int(x) for x in input().split()] for i in range(1,n): a[i]=a[i]+a[i-1] for x in labels: print(func(a,x))
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(input()) worms = list(map(int, input().split(' '))) m = int(input()) labels = list(map(int, input().split(' '))) pile_labels = [] for i in range(len(worms)): if i == 0: pile_labels.append(worms[0]) else: value = pile_labels[i - 1] + worms[i] pile_labels.append(value) for i in labels: answer = bisect.bisect_left(pile_labels, i) print(answer + 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeMap; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class b271 { static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n=in.nextInt(); long arr[]=new long[n]; arr[0]=in.nextLong(); for(int i=1;i<n;i++) { arr[i]=arr[i-1]+in.nextLong(); } int m=in.nextInt(); for(int i=0;i<m;i++) { int temp=in.nextInt(); int ans=Arrays.binarySearch(arr,temp); if(ans<0) { ans=~ans; } out.println(ans+1); } } } static int binarysearch(long[] a, int start, int end, int x) { if (x < a[start]) return start+1; else if(x==a[start]) return start+1; if (x >= a[end]) return end+1; int mid = (start + end)/2; if (x < a[mid]) return binarysearch(a, start, mid, x); else return binarysearch(a, mid+1, end, x); } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static long min(long x,long y){return x<y?x:y;} static long min(long x,long y,long z){return x<y?(x<z?x:z):(y<z?y:z);} static long max(long x,long y){return x>y?x:y;} static long max(long x,long y,long z){return x>y?(x>z?x:z):(y>z?y:z);} static int min(int x,int y){return x<y?x:y;} static int min(int x,int y,int z){return x<y?(x<z?x:z):(y<z?y:z);} static int max(int x,int y){return x>y?x:y;} static int max(int x,int y,int z){return x>y?(x>z?x:z):(y>z?y:z);} static int abs(int x){if(x>=0)return x;else return -x;} static long abs(long x){if(x>=0)return x;else return -x;} 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 String nextLine() { String fullLine=null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine=reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(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
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; int arr[n]; int z = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; z += arr[i]; arr[i] = z; } cin >> m; for (int j = 0; j < m; j++) { cin >> z; z = lower_bound(arr, arr + n, z) - arr; cout << z + 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
from itertools import accumulate def bin_search(x, left, right, num_list): if right - left <= 1: if x <= num_list[left]: return left + 1 else: return right + 1 else: middle = (left + right) // 2 if x < num_list[middle]: return bin_search(x, left, middle, num_list) elif x > num_list[middle]: return bin_search(x, middle + 1, right, num_list) else: return middle + 1 n = int(input()) counter = 1 heap = 0 worms = [0] for num in input().split(): heap += 1 for i in range(counter, counter + int(num)): worms.append(heap) m = int(input()) for request in input().split(): print(worms[int(request)])
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 binSearch(int a, int* numSet, int setSize) { int initial = 0; int final = setSize - 1; int i = 0; while (true) { i++; if (i == 20) break; int mid = initial + (final - initial) / 2; if (final - initial <= 1) { if (a < numSet[final] && a > numSet[initial]) return final + 1; else if (a <= numSet[initial]) return initial + 1; else return final + 1; } if (numSet[mid] > a) { final = mid; } else if (numSet[mid] < a) { initial = mid; } else { return mid + 1; } } return 0; } int main() { int n; cin >> n; int* cumuArray = new int[n]; for (int i = 0; i < n; i++) { cumuArray[i] = 0; int t; cin >> t; if (i == 0) cumuArray[i] = t; else cumuArray[i] = cumuArray[i - 1] + t; } int numJuicy = 0; cin >> numJuicy; for (int i = 0; i < numJuicy; i++) { int t; cin >> t; int pileInd = binSearch(t, cumuArray, n); cout << pileInd << 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 a[100005]; int main() { int n, m, k; while (scanf("%d", &n) != EOF) { a[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] += a[i - 1]; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &k); printf("%d\n", lower_bound(a, a + n, k) - a); } } 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(input()) ls = [] zzh = [] zzh.append(0) ls = [int(u) for u in input().split()] for item in range(len(ls)): for _ in range(ls[item]): zzh.append(item+1) m = int(input()) inp = [int(u) for u in input().split()] for u in inp: print(zzh[u])
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.awt.Point; import java.util.Scanner; public class imp { public static void main(String[] args) { Scanner s = new Scanner (System.in); // get number of piles // max n -> 10^5 int n = s.nextInt(); int read; // max ai -> 10^6 Point[] piles = new Point [n+1]; piles[0] = new Point(0,0); for (int i=1;i<=n;i++){ piles[i] = new Point (); read = s.nextInt(); piles[i].x = piles[i-1].y + 1; piles[i].y = piles[i-1].y + read; } // max m -> 10^5 int m = s.nextInt(),label; // max q -> 10^6 int lo,hi,mid; while (m-- > 0){ label = s.nextInt(); lo=1;hi=n; while (hi >= lo){ mid = (hi+lo ) /2; if (piles[mid].x > label ) hi = mid-1; else if (piles[mid].y < label) lo = mid+1; else { System.out.println(mid); break; } } } } }
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()) b = list(map(int, input().split())) res = [] for i in range(n): res += [i] * a[i] for x in b: print(res[x-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; vector<int> vec; int main() { long long int n, sum = 0, t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; sum += t; vec.push_back(sum); } cin >> n; for (int i = 0; i < n; i++) { cin >> t; if (t <= vec[0]) { cout << "1\n"; continue; } cout << lower_bound(vec.begin(), vec.end(), t) - vec.begin() + 1 << "\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
import java.util.Scanner; public class Worms { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, m; int[] a; n = sc.nextInt(); a = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { sum += sc.nextInt(); a[i] = sum; } m = sc.nextInt(); for (int i = 0; i < m; i++) { int search = sc.nextInt(); int mid, low = 0, high = n - 1; mid = high / 2; // Get the mid-point // Binary Search for answer while (true) { if (mid > 0) { if (search > a[mid - 1] && search <= a[mid]) { System.out.println(mid + 1); break; } else if (a[mid] < search) { // search greater than current guess low = mid + 1; } else { // search less than current guess high = mid - 1; } mid = (high + low) / 2; } else { if (search <= a[low]) System.out.println(1); else System.out.println(2); break; } } } sc.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
#include <bits/stdc++.h> using namespace std; int n, a[100000], m, q, sp[100000]; int cautbin(int val) { int left = 0, right = n - 1, best = -1; while (left <= right) { int mid = (left + right) / 2; if (sp[mid] >= val) { best = mid; right = mid - 1; } else { left = mid + 1; } } return best; } int main() { cin >> n; cin >> a[0]; sp[0] = a[0]; for (int i = 1; i < n; ++i) { cin >> a[i]; sp[i] = a[i] + sp[i - 1]; } cin >> m; for (int i = 0; i < m; ++i) { cin >> q; cout << 1 + cautbin(q) << "\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 mod = 1e9 + 7; void solve() { int n; cin >> n; vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } int m; cin >> m; while (m--) { int x; cin >> x; int l = -1, r = n + 1; while (r - l > 1) { int m = (l + r) / 2; if (a[m] <= x) l = m; else r = m; } if (x - a[l] == 0) cout << l << "\n"; else cout << l + 1 << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 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
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ar[n], s[n]; int sum = 0; for (int i = 0; i < n; i++) { cin >> ar[i]; sum += ar[i]; if (i == 0) s[i] = 1; else s[i] = s[i - 1] + ar[i - 1]; } int st[sum + 1]; int p = 1; for (int i = 0; i < n; i++) { for (int j = s[i] - 1; j < s[i] + ar[i] - 1; j++) { st[j] = p; } p++; } int k, d; cin >> k; for (int i = 0; i < k; i++) { cin >> d; cout << st[d - 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
N = input() A = list(map(int, raw_input().split(' '))) M = input() Q = list(map(int, raw_input().split(' '))) B = [A[0]] for i in xrange(N-1) : B.append(A[i+1]+B[-1]) for i in Q : l = 0 r = len(B)-1 while True : if l == r : break m = (l+r)//2 if i <= B[m] : r = m else : l = m+1 print(r+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
#include <bits/stdc++.h> using namespace std; int main() { int n, w; cin >> n; int a[n], sum[n]; sum[0] = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum[i + 1] = sum[i] + a[i]; } int j; n += 1; cin >> w; while (w--) { cin >> j; cout << (lower_bound(sum, sum + n, j) - sum) << 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
n = int(raw_input()) a = map(int, raw_input().split()) m = int(raw_input()) q = map(int, raw_input().split()) """ 5 2 7 3 4 9 3 1 25 11 idx 0 1 2 3 4 5 prefix_sums: 0 2 9 12 16 25 """ prefix_sums = [0] curr_sum = 0 for i in xrange(len(a)): curr_sum+=a[i] prefix_sums.append(curr_sum) #check: if bucket bth bucket contains qi. def in_bucket(b, qi): if qi <= prefix_sums[b] and qi > prefix_sums[b-1]: return 0 elif qi > prefix_sums[b]: return -1 else: return 1 # 2 element: #[0,3] # left = 1, right = 3 #binary search on buckets (buckets vary). def bin_search(qi): left = 0 right = len(prefix_sums) while (left+1 < right): mid = (left+right)/2 bucket = prefix_sums[mid] if bucket < qi: left = mid elif bucket > qi: right = mid else: return mid return right for qi in q: print bin_search(qi)
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
g=int(input()) b = [int(x) for x in input().split()] lookup=[] group_no=0 for x in b: group_no +=1 for j in range(x): lookup.append(group_no) c=int(input()) d=[int(x) for x in input().split()] for x in d: print(lookup[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
def main_tle(): raw_input() A = [int(i) for i in raw_input().split(" ")] raw_input() B = [int(i) for i in raw_input().split(" ")] Bv2i = {} for i in range( len(B) ): if B[i] not in Bv2i: Bv2i[B[i]] = [] Bv2i[B[i]].append(i) B.sort() R = [0]*len(B) pn = 1 pm = A[pn-1] for i in B: while i > pm: pn += 1 pm += A[pn-1] for index in Bv2i[i]: R[index] = pn for i in R: print i def BS(A,mini,maxi,val): #print "in",mini,maxi,val if maxi - mini < 3: for i in range(mini,maxi+1): if A[i] >= val: return i+1 mid = (mini+maxi)/2 if A[mid] < val: return BS(A,mid,maxi,val) else: return BS(A,mini,mid,val) def BS2(A,mini,maxi,val): while maxi-mini > 3: mid = (mini+maxi)/2 if A[mid] < val: mini = mid else: maxi = mid for i in range(mini,maxi+1): if A[i] >= val: return i+1 def main_bs(): raw_input() A = [int(i) for i in raw_input().split(" ")] raw_input() B = [int(i) for i in raw_input().split(" ")] intA = [] for i in A: try: intA.append(intA[-1]+i) except: intA.append(i) #print intA for i in B: #print "val",i res = BS2(intA,0,len(A)-1,i) print res main_bs()
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; signed main() { long long n, m, x, ans = 0; cin >> n; long long arr[n + 1]; arr[0] = 1; cin >> arr[1]; for (long long i = 2; i < n + 1; i++) { cin >> x; arr[i] = arr[i - 1] + x; } cin >> m; long long q[m]; for (long long i = 0; i < m; i++) { cin >> q[i]; if (q[i] == 1) cout << "1" << "\n"; else cout << lower_bound(arr, arr + n + 1, q[i]) - arr << "\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
# num piles n = int(raw_input()) piles = raw_input().split() m = int(raw_input()) queries = raw_input().split() piles = [ int(x) for x in piles ] queries = [ int(x) for x in queries ] #algorithm - make an array of size sum(piles) total = sum(piles) storage = [0]*total #fill in the indexes counter = 0 for i in range(n): curr = piles[i] #current num for j in range(curr): storage[counter] = i+1 counter += 1 #answr th queries for i in range(m): q = queries[i] print storage[q-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
#include <bits/stdc++.h> using namespace std; int bs(int ar[], int le, int ri, int x) { int mid; mid = (le + ri) / 2; if (ri >= le) { if (x == ar[mid]) return mid; else if (x < ar[0]) return 0; else if (x < ar[mid] && x > ar[mid - 1]) { return mid; } else if (x > ar[mid] && x < ar[mid + 1]) { return mid + 1; } else if (x > ar[mid]) { return bs(ar, le = mid + 1, ri, x); } else { return bs(ar, le, ri = mid - 1, x); } } } int main() { int n, m, s = 0, x, l, r; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 1; i < n; i++) a[i] += a[i - 1]; scanf("%d", &m); int b[m]; for (int i = 0; i < m; i++) scanf("%d", &b[i]); l = 0; r = n - 1; for (int i = 0; i < m; i++) { printf("%d\n", bs(a, l, r, b[i]) + 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 sys import stdin, stdout from itertools import repeat def main(): n = int(stdin.readline()) a = map(int, stdin.readline().split(), repeat(10, n)) m = int(stdin.readline()) q = map(int, stdin.readline().split(), repeat(10, m)) q = [(x, i) for i, x in enumerate(q)] q.sort() ans = [0] * m i, s = 0, 0 for x, j in q: while x > s: s += a[i] i += 1 ans[j] = i stdout.write('\n'.join(map(str, ans))) 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 a[1000003], n, x = 1, t; cin >> n; for (int i = 1; i < n + 1; i++) { cin >> t; while (t--) { a[x] = i; x++; } } int m; cin >> m; for (int i = 0; i < m; i++) { cin >> t; cout << a[t] << 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()) totalWorms = [] totalWorms.append(numberOfWorms[0]) for i in range(1,len(numberOfWorms)): totalWorms.append(numberOfWorms[i] + totalWorms[i-1]) # print(totalWorms) # print(juicyWorms) for i in juicyWorms: start = 0 end = len(totalWorms) - 1 while start <= end: mid = (end - start) / 2 + start # if totalWorms[mid] >= i: # print mid # break if totalWorms[mid] < i: start = mid + 1 else: end = mid - 1 print start + 1 # for index, value in enumerate(totalWorms): # if i <= value: # print index+1 # break
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
n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) d = {} t = 1 for i in range(n): for j in range(t, t + a[i]): d[j] = i + 1 t += a[i] for i in range(m): print(d[b[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
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, numbers1; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> numbers1; a[i] = numbers1; } int m, numbers2; cin >> m; int q[m]; for (int i = 0; i < m; i++) { cin >> numbers2; q[i] = numbers2; } int labeled_ants_array[n]; int suma_acumulada = 0; for (int j = 0; j < n; j++) { suma_acumulada += a[j]; labeled_ants_array[j] = suma_acumulada; } int p; int r; for (int i = 0; i < m; i++) { int k = q[i]; p = 0; r = n - 1; while (p < r) { int mid = (p + r) / 2; if (k <= labeled_ants_array[mid]) { r = mid; } else { p = mid + 1; } } cout << (p + 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
''' my bibary search didn't work ''' # ossan def bin_search(li, k, l, h): m = (l + h) // 2 if k == li[m]: return m+1 elif li[m-1] < k < li[m] and m != 0: return m+1 elif k < li[m] and m == 0: return m+1 elif k > li[m] and m == len(li)-1: return m+1 elif k > li[m]: return bin_search(li, k, m + 1, h) else: return bin_search(li, k, l, m - 1) n = int(input()) worm_piles = list(map(int, input().split())) for i in range(1, n): worm_piles[i] += worm_piles[i - 1] m = int(input()) for i in input().split(): t = int(i) print(bin_search(worm_piles, t, 0, n-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()) piles = list(map(int, input().split()))[:n] juicy_number = int(input()) juicy = list(map(int, input().split()))[:juicy_number] worms = [piles[0]] j = 0 for i in range(1, len(piles)): number_two = worms[j] + piles[i] worms.append(number_two) j += 1 def binarySearch(arr, l, r, x): while l <= r: mid = (l+r)//2 if arr[mid] == x: return mid + 1 if arr[mid] > x and arr[mid-1] < x: return mid + 1 if arr[mid] < x and arr[mid+1] > x: return mid + 2 if arr[mid] > x: r = mid-1 else: l = mid+1 return mid + 1 for i in juicy: print(binarySearch(worms, 0, len(worms)-1, 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.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub int n; int[] ai; Scanner sc = new Scanner(System.in); n = sc.nextInt(); ai = new int[n]; for (int i = 0; i < n; i++) { ai[i] = sc.nextInt(); } for (int i = 1; i < n; i++) { ai[i] += ai[i - 1]; } int searchNum = sc.nextInt(); int[] searchList = new int[searchNum]; for (int i = 0; i < searchNum; i++) { searchList[i] = sc.nextInt(); } sc.close(); for (int i = 0; i < searchNum; i++) { int head = 0, tail = n - 1; while ((tail - head) > 1) { int tmp = (tail + head) / 2; if (searchList[i] > ai[tmp]) { head = tmp; } else { tail = tmp; } } if (ai[head] >= searchList[i]) System.out.println(head + 1); else System.out.println(tail + 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
from sys import stdin def main(): arr=[] cnte=1 cnte2=1 arr.append(int(0)) stdin.readline() for i in map(int,stdin.readline().split()): for j in range(0,i): arr.append(cnte2) cnte2=cnte2+1 stdin.readline() for i in map(int,stdin.readline().split()): print(arr[i],'\n') 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(raw_input()) p=map(int,raw_input().split()) m=int(raw_input()) a=map(int,raw_input().split()) K=[0] for i in range(n): K.extend([i+1]*p[i]) for j in a: print K[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
import bisect n = int(input()) vals = [*map(int, input().split())] m = int(input()) queries = [*map(int, input().split())] # Finding cumulative for i in range(1, n): vals[i] += vals[i-1] for query in queries: print(bisect.bisect_left(vals, query) + 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; long st[100005]; long bSearch(long v, long s, long e) { long mid; while (s <= e) { mid = (s + e) / 2; if (st[mid] == v) return mid; else if (st[mid] <= v && st[mid + 1] > v) return mid; else if (st[mid] > v && st[mid + 1] > v) { e = mid - 1; } else { s = mid + 1; } } } int main() { long n, m, a, i; ios_base::sync_with_stdio(0); while (cin >> n) { long cnt = 1; st[1] = 1; for (i = 2; i <= n + 1; i++) { cin >> a; st[i] = st[i - 1] + a; } cin >> m; for (i = 1; i <= m; i++) { cin >> a; cout << bSearch(a, 1, 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
def sortFst(val): return val[0] def sortScd(val): return val[1] a = int(input()) b = input().split() c = int(input()) d = input().split() dtrix = [] for i in range(len(d)): dtrix.append([int(d[i]), i, 0 ]) dtrix.sort(key = sortFst) matrix = [] atual = 0 for i in b: matrix.append([atual+1, atual + int(i)]) atual += int(i) last = 0 for i in range(len(dtrix)): for ii in range(last, len(matrix), 1): if int(dtrix[i][0]) >= int(matrix[ii][0]) and int(dtrix[i][0]) <= int(matrix[ii][1]): dtrix[i][2] = ii last = ii break dtrix.sort(key = sortScd) for i in dtrix: print(i[2]+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 java.util.*; public class Solution { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; int sum=0; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); sum=sum+arr[i]; } int dp[]=new int[sum+1]; int l=0; for(int i=0;i<n;i++) { for(int j=1;j<=arr[i];j++) { if(i==0) { dp[j]=i+1; } else { dp[l+j]=i+1; } } l=l+arr[i]; } int m=sc.nextInt(); for(int i=0;i<m;i++) { int g=sc.nextInt(); System.out.println(dp[g]); } } }
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,raw_input().split())) N2=int(input()) A2=list(map(int,raw_input().split())) con=0 Numeros=1 Matriz=[] for k in A: con+=1 for j in range(k): Matriz.append([Numeros,con]) Numeros+=1 for i in A2: print Matriz[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
n, a, m, b, e = input(), [int(i) for i in input().split()], input(), [int(i) for i in input().split()], [] for i in range(len(a)): for j in range(a[i]): e.append(i+1) for i in b: print(e[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
from sys import stdin,stdout inpt = lambda: stdin.readline().strip() prnt = lambda x: stdout.write(str(x)+'\n') def find_res(a,q): Li = [] r = 1 for i in a: Li+=[r]*i r+=1 for i in q: prnt(Li[i-1]) def main(): inptf = inpt inptf() a = map(int,inpt().split()) inptf() q = map(int,inpt().split()) find_res(a,q) 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
from bisect import bisect_left n = int(input()) worms = list(map(int, input().split())) worms_count = [0] for w in worms: worms_count.append(worms_count[-1] + w) # print(*worms_count) m = int(input()) juicy = list(map(int, input().split())) for j in juicy: print(bisect_left(worms_count, j))
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
piles = int(input()) number_of_worm_in_each_pile = [int(i) for i in input().split()] number_of_juicy_worm = int(input()) label_of_juicy_worm = [int(i) for i in input().split()] pile = 1 worm_repo = [] counter = 0 for i in range(piles): for j in range(1,number_of_worm_in_each_pile[counter]+1): worm_repo.append(pile) pile += 1 counter += 1 for i in label_of_juicy_worm: print(worm_repo[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.Arrays; public class Worms { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); long sum[] = new long[n]; String input[] = br.readLine().split(" "); sum[0] = Integer.parseInt(input[0]); for(int i = 1;i<n;i++) { sum[i] += sum[i-1] + Integer.parseInt(input[i]); } int m = Integer.parseInt(br.readLine()); String queries[] = br.readLine().split(" "); for(int i = 0;i<m;i++) { long q = Long.parseLong(queries[i]); long ans = Arrays.binarySearch(sum, q); if(ans < 0) System.out.println(Math.abs(ans)); else System.out.println(ans + 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
import java.io.*; import java.util.Scanner; public class pfffff { static PrintWriter out = new PrintWriter(System.out); static Scanner in = new Scanner(System.in); 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(); } }
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 n = int(input()) ls = list(map(int,input().split())) for i in range(1,n): ls[i]+= ls[i-1] q = int(input()) lq = list(map(int,input().split())) for item in lq: print(bisect.bisect_left(ls,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
num1 = int(input()) list1 = [int(k) for k in input().split()] num2 = int(input()) list2 = [int(k) for k in input().split()] sum_list = [0] * (num1 + 1) for k in range(num1): sum_list[k+1] = sum_list[k] + list1[k] def binary_search(list, num): first_bound = 0 last_bound = len(list) - 1 while True: temp = int((first_bound + last_bound)/2) if temp == first_bound: return last_bound else: if list[temp] < num: first_bound = temp else: last_bound = temp output_list = [binary_search(sum_list, num) for num in list2] for k in output_list: print(k)
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-1] from bisect import bisect_left for i,x in enumerate(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
#include <bits/stdc++.h> using namespace std; int a[100001]; int main() { int n; cin >> n; int sum = 1; vector<int> v; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; v.push_back(sum); } v.push_back(sum + 1); int q; cin >> q; for (int i = 0; i < q; i++) { int x; scanf("%d", &x); int ans = upper_bound(v.begin(), v.end(), x) - v.begin() + 1; printf("%d\n", ans); } 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(input()) arr = list(map(int,input().split())) for i in range(1,n): arr[i]+=arr[i-1] q = int(input()) query = list(map(int,input().split())) d = {} for i in query: d[i] = 0 temp = query[::] temp.sort() ans = [] cnt = 0 for i in temp: while i>arr[cnt]: cnt+=1 d[i] = cnt+1 for i in query: print(d[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
def bs(v, a): lo, hi = 0, len(a) - 1 r = -1 while lo <= hi: mi = lo + (hi - lo)//2 if a[mi] >= v: r = mi hi = mi - 1 else: lo = mi + 1 return r n = int(input() ) a = list(map(int, input().split() ) ) for i in range(1, n): a[i] += a[i-1] input() for x in list(map(int, input().split() ) ): print(1 + bs(x, a) )
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 java.util.Scanner; public class Main { static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static int nextInt()throws IOException{ in.nextToken(); return (int)in.nval; } static long nextLong()throws IOException{ in.nextToken(); return (long)in.nval; } static double nextDouble()throws IOException{ in.nextToken(); return in.nval; } static String next()throws IOException{ in.nextToken(); return in.sval; } // static int tt; static int[] c=new int[100002]; static int[] b=new int[100002]; static void qsort(int x,int y) { int l=x,r=y,m=b[(x+y)>>1],t; do { while (b[l]<m) l++; while (b[r]>m) r--; if (l<=r) { t=b[l]; b[l]=b[r]; b[r]=t; // t=b[l]; // b[l]=b[r]; // b[r]=t; l++; r--; } } while (l<=r); if (l<y) qsort(l,y); if (x<r) qsort(x,r); } public static void main(String[] args) throws IOException{ // while (in.nextToken() != StreamTokenizer.TT_EOF) { // tt=(int)in.nval; // for (int t=1;t<=tt;t++) // { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); int[] a=new int[100002]; a[0]=0; for (int i=1;i<=n;i++) { a[i]=scan.nextInt(); a[i]+=a[i-1]; } int m=scan.nextInt(); for (int i=1;i<=m;i++) { b[i]=scan.nextInt(); c[i]=b[i]; } int[] d=new int[1000002]; int f=1; qsort(1,m); for (int i=1;i<=m;i++) { while (a[f]<b[i]) f++; d[b[i]]=f; } for (int i=1;i<=m;i++) System.out.println(d[c[i]]); // out.flush(); // } // } } }
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.util.*; import java.io.*; public class TaskB { private FastScanner in; private PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); int[] num = new int[(int) 2e6]; int cur = 0; for (int i = 0; i < n; i++) { int val = in.nextInt(); for (int j = cur + 1; j <= cur + val; j++) { num[j] = i + 1; } cur += val; } int m = in.nextInt(); for (int i = 0; i < m; i++) { out.println(num[in.nextInt()]); } } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } private class FastScanner { private BufferedReader br; private StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] arg) { new TaskB().run(); } }
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; const int M = 1000 * 1000 + 1000; int n; int a[M]; int m; int q[M]; int sum[M]; int search(int f, int e, int k) { if (f == e) return f; int mid = (f + e) / 2; if (sum[mid] >= k) return search(f, mid, k); return search(mid + 1, e, k); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cin >> m; for (int i = 0; i < m; i++) cin >> q[i]; sum[0] = a[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + a[i]; for (int i = 0; i < m; i++) cout << search(0, n, q[i]) + 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
n = int(input()) worms = [int(x) for x in input().split()] m = int(input()) jucy = [int(x) for x in input().split()] label = [] i = 1 for x in worms: for j in range (0, x): label.append(i) i+=1 for x in jucy: print(label[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 sys input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) n_worms = inp() worms = inlt() n_juice = inp() juicy = inlt() def createBuckets(worms): res = [] prev_sum = 0 for i in range(len(worms)): worm = worms[i] res.append(prev_sum + worm) prev_sum += worm return res buckets = createBuckets(worms) def binarySearch(buckets, juice): i, j = 0, len(buckets) - 1 while i < j: m = i + (j - i) // 2 if buckets[m] >= juice: j = m else: i = m + 1 return i for juice in juicy: print(binarySearch(buckets, juice) + 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 = input() a = map(int,raw_input().split()) m = input() q = map(int,raw_input().split()) f = [0] for i in range(n): f += [i+1]*a[i] for i in q: print f[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
n = int(input()) l = list(map(int,input().split())) a = [] k = 1 mp = {} for i in range(n): for j in range(k,k+l[i]): mp[j] = i+1 k = k + l[i] m = int(input()) q = list(map(int,input().split())) for value in q: print(mp[value])
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 Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); int [] n = new int [input.nextInt()] ; int sum =0; for (int i =0 ; i <n.length ; i++){ n[i]= input.nextInt(); sum+=n[i];} int [] x = new int [sum]; int in=0; for (int i = 0; i < n.length; i++) { for (int j = 0; j < n[i]; j++) { x[in]=i+1; in++; }} int m = input.nextInt(); for (int i =0 ; i <m ; i++) {System .out.println(search(x,input.nextInt()-1, 0 , x.length-1));} } public static int search(int [] x , int key , int l , int h ){ //System.out.println(l+" "+h); while (h>=l){ int mid = (l+h)/2; // System.out.println(mid); if (key <mid ) return search (x, key , l , mid-1); else if (key >mid ) return search (x, key ,mid +1 , h); else return x[mid]; } return -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
# -*- coding: utf-8 -*- #α••β”Œβ—•α—œβ—•β”α•— HELLO HELLO HELLO α••β”Œβ—•α—œβ—•β”α•— n = int(raw_input()) a = list(int(i) for i in raw_input().strip().split(" ")) m = int(raw_input()) q = list(int(i) for i in raw_input().strip().split(" ")) t = range(n+1) t[0] = 0 for i in range(1,n+1): t[i] = a[i-1] + t[i-1] for i in q: s = 1 e = n m = (s+e)/2 while not (t[m-1] < i and t[m] >= i): if t[m] < i: s = m m = (s+e+1)/2 else: e = m m = (s+e)/2 print m
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
trash = int(input()); firstLine = [int(x) for x in input().split()]; trash = int(input()); secondLine = [int(x) for x in input().split()]; worms = []; for i in range(0, len(firstLine)): for j in range(0, firstLine[i]): worms.append(i); for i in secondLine: print(worms[i-1]+1); '''for i in range(1, len(firstLine)): firstLine[i]+=firstLine[i-1]; #print(firstLine); def findWorm(a, bottom, top): middle = int(bottom+(top-bottom)/2); if(middle==0): return(0); if(a <= firstLine[middle] and a > firstLine[middle-1]): return(middle); elif(a <= firstLine[middle] and a <= firstLine[middle-1]): return findWorm(a, bottom, middle-1); elif(a > firstLine[middle]): return findWorm(a, middle+1, top); for i in secondLine: print(findWorm(i, 0, len(firstLine)-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=input();rows=map(int,raw_input().split());q=input() quer=sorted(enumerate(map(int,raw_input().split())),key=lambda x:x[1]) ans=[0]*q;sums=[rows[0]]+[0]*(n-1);ct=0 for i in xrange(1,n): sums[i]=sums[i-1]+rows[i] for k,v in quer: while sums[ct]<v: ct+=1 ans[k]=str(ct+1) print "\n".join(ans)
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.*; import java.io.*; public class Main { static int[][] grid; static int n ; static ArrayList<Integer> adj ; static HashMap<Integer,Integer> hm; public static void main( String args[] ) { MyScanner sc = new MyScanner(); n = sc.nextInt(); hm = new HashMap<Integer,Integer>(); int counter = 1 ; for( int i = 1 ; i <= n ; ++i) { int temp = sc.nextInt(); for( int j = 0 ; j < temp ; ++j ) { hm.put( counter++ , i ); } } int q = sc.nextInt(); for( int i = 0 ; i < q ; ++i) { System.out.println( hm.get( sc.nextInt())); } } static class node { int parent ; ArrayList<Integer> adj; public node() { } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in),8*1024); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class MyWriter { BufferedWriter bw; public MyWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out),8*1024); } void print(String s) { } void println(String s) { try { bw.write( s , 0 , s.length() ); bw.newLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } void close() { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
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> const int MAXN = 100 + 10; const int MAXT = 100000 + 10; const int INF = 0x7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; using namespace std; int n, a[MAXT], l[MAXT], r[MAXT], q; void init() { l[0] = 1; r[0] = a[0]; for (int i = 1; i < n; ++i) { l[i] = r[i - 1] + 1; r[i] = l[i] + a[i] - 1; } } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i); init(); scanf("%d", &q); int tmp; while (q--) { scanf("%d", &tmp); int lur = lower_bound(r, r + n, tmp) - r; printf("%d\n", lur + 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
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct query { int x, id; } q[N]; int n; int a[N]; int sum = 0, uk = 0; int m; int ans[N]; bool cmp(query A, query B) { return A.x < B.x; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &q[i].x); q[i].id = i; } sort(q + 1, q + m + 1, &cmp); for (int i = 1; i <= m; ++i) { while (sum < q[i].x) { ++uk; sum += a[uk]; } ans[q[i].id] = uk; } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); 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 from bisect import bisect_left from bisect import bisect_right n = input() a = map(int, raw_input().split()) m = input() for i in range(1, n): a[i] += a[i-1] a.insert(0, 0) for i in map(int, raw_input().split()): print bisect.bisect_left(a, 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
import sys input = sys.stdin.readline def main(): int(input()) heaps = list(map(int, input().split())) sum = 0 i = 0 while i < len(heaps): heaps[i] = sum + heaps[i] sum = heaps[i] i += 1 int(input()) es = list(map(int, input().split())) for e in es: print(binarySearch(heaps, e) + 1) def binarySearch(arr, n): first = 0 last = len(arr) - 1 if (n > arr[0]) and (n <= arr[1]): return 1 while first <= last: mid = (first + last) // 2 if (mid - 1) < 0: return mid if (n > arr[mid - 1]) and (n <= arr[mid]): return mid if arr[mid] > n: last = mid - 1 else: first = mid + 1 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()) p = {} pn = 1 for i, n in enumerate(input().split()): n = int(n) for ni in range(pn, pn + n): p[ni] = i + 1 pn = pn + n j = int(input()) for jw in input().split(): jw = int(jw) print(p[jw])
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
num_piles = int(input()) worm_piles = list(map(int, input().split())) num_juicy_worms = int(input()) juicy_worm_labels = list(map(int, input().split())) bins = [] # ith num is min for the bin running_sum = 1 for i in range(1, num_piles + 1): if i == 1: bins.append(running_sum) running_sum += worm_piles[i - 1] else: bins.append(running_sum) running_sum += worm_piles[i - 1] # Output the bin the juicy worms are in last_bin_idx = num_piles - 1 for juicy_worm_label in juicy_worm_labels: if juicy_worm_label >= bins[last_bin_idx]: # don't need to do binary search since it's last print(last_bin_idx + 1) continue mn = 0 mx = last_bin_idx while mn <= mx: mid_idx = mn + (mx - mn) // 2 if bins[mid_idx] <= juicy_worm_label < bins[mid_idx + 1]: print(mid_idx + 1) break if juicy_worm_label > bins[mid_idx]: # want to check greater vals mn = mid_idx + 1 else: # want to check lower vals mx = mid_idx - 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 main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int p[n]; cin >> p[0]; for (int i = 1; i < n; i++) { int x; cin >> x; p[i] = p[i - 1] + x; } int t; cin >> t; while (t--) { int key; cin >> key; int lb = 0, ub = n - 1, mid = (lb + ub) / 2, flag = 0; while (ub >= lb) { if (key == p[mid]) { flag++; break; } else if (key > p[mid]) { lb = mid + 1; mid = (lb + ub) / 2; } else { ub = mid - 1; mid = (lb + ub) / 2; } } if (flag > 0) cout << (mid + 1) << endl; else { if (key > p[mid]) cout << mid + 2 << endl; else cout << mid + 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
a=int(input()) b= list(map(int,input().split())) c=int(input()) d= list(map(int,input().split())) for k in range(1,a): b[k] += b[k-1] for j in d: l=0 r= a-1 while l<= r: if j <= b[0]: mid=0 break mid= (l+r)//2 if b[mid] >= j and b[mid-1] < j: break elif b[mid] > j: r=mid-1 else: l=mid+1 print(mid+1)
PYTHON3