iT邦幫忙

0

<<Variable Sized Arrays>> _HakerRank_C++_Introduction

  • 分享至 

  • xImage
  •  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    
    int vecrow , querys ;
    cin >> vecrow >> querys;
    
    vector<vector<int>> vec ;
    vec.resize(vecrow);

    for(int i = 0 ; i < vecrow ; i++)
    {
        int veccolumn, memnum;
        cin >> veccolumn ;
        vec[i].resize(veccolumn);
        
        for(int j = 0; j < veccolumn ; j++)
        {
            cin >> memnum ;
            vec[i][j] =  memnum ;
        }
    }
    
    int row, colum ;        
    for(int i = 0 ; i < querys ; i++)
    {
        cin >> row >> colum ;
        cout << vec[row][colum] << endl ;
    }
     
    return 0;
}

Consider an n-element array, a, where each index i in the array contains a reference to an array of Kᵢ integers (where the value of Kᵢ varies from array to array). See the Explanation section below for a diagram.

Given a, you must answer q queries. Each query is in the format i j, where i denotes an index in array a and j denotes an index in the array located at a[i]. For each query, find and print the value of element j in the array at location a[i] on a new line.

Click here to know more about how to create variable sized arrays in C++.

Input Format
The first line contains two space-separated integers denoting the respective values of n (the number of variable-length arrays) and q (the number of queries).
Each line i of the n subsequent lines contains a space-separated sequence in the format
k a[i]₀ a[i]₁ … a[i]k₋₁
describing the k-element array located at a[i].

Each of the q subsequent lines contains two space-separated integers describing the respective values of i (an index in array a) and j (an index in the array referenced by a[i]) for a query.

Constraints
.1 <= n <= 10^5
.1 <= q <= 10^5
.1 <= k <= 3 * 10^5
.n <= Σk <= 3 * 10^5
.0 <= i < n
.0 <= j < k
.All indices in this challenge are zero-based.
.All the given numbers are non negative and are not greater than 10^6

Output Format
For each pair of i and j values (i.e., for each query), print a single integer that denotes the element located at index j of the array referenced by a[i]. There should be a total of q lines of output.

Sample Input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3

Sample Output
5
9

Explanation
The diagram below depicts our assembled Sample Input:

We perform the following q = 2 queries:

  • Find the array located at index i = 0, which corresponds to a[0] = [1, 5, 4]. We must print the value at index j = 1 of this array which, as you can see, is 5.
  • Find the array located at index i = 1, which corresponds to a[1] = [1, 2, 8, 9, 3]. We must print the value at index j = 3 of this array which, as you can see, is 9.

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言