Last Semester PE - Q1: Where to Insert X? First, you are...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Last Semester PE Q1: Where to Insert X? First, you are presented with a number L, (1 <= L <= 1,000,000). Then, you are given an UNSORTED list of L non-negative (>=0) 32-bit integers (2 32 - 1). (You will need to sort this list first.) Then, a number Q follows, which represent Q queries, (1 <= Q <= 10,000). Next, Q lines follow. For each line, you will see an integer X. We want to insert this X into the SORTED list of these L integers in such a way that if we insert X into that index, the insertion will not cause the resulting list to be unsorted. Where to Insert X? The answer for each query is an integer in one line that tells the FIRST INDEX of the proper insertion point of X in this format: "The insertion index is : <THE_INDEX>" • If you do linear search on the sorted list to find the insertion point, you will fail some of the cases.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Input // explanation ******************************************************** 5 // L=5 numbers 5 3 2 4 10 // the L numbers in one line 4 // Q=4 queries 0 // query 1 4 // query 2 6 // query 3 11 // query 4 ******************************************************** Output // for explanation see below ******************************************************** The insertion index is : 0 The insertion index is : 2 The insertion index is : 4 The insertion index is : 5 ******************************************************** #include <stdio.h> #include <algorithm> using namespace std; int arr[1000001], n, q, num; void printLowerBoundPos(){ printf("The insertion index is : "); for (int i=0; i<n; i++) if (arr[i]>=num){ printf("%d\n",i); return; } printf("%d\n",n); } int main() { scanf("%d",&n); for (int i=0; i<n; i++) scanf("%d",&arr[i]);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Last Semester PE - Q1: Where to Insert X? First, you are...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online