📌  相关文章
📜  数组消失且元素重新出现的查询

📅  最后修改于: 2021-04-24 21:51:17             🧑  作者: Mango

给定大小为N的数组arr [] 。每秒钟,整数消失N秒,并在N秒后重新出现在其原始位置。整数从左到右依次消失arr [0],arr [1],…,arr [N – 1] 。在所有整数消失之后,它们开始重新出现,直到所有整数重新出现。一旦再次显示N个元素,该过程将再次开始。
现在给定Q个查询,每个查询由两个整数tM组成。的任务是确定的M个从左边第i个元素在t第二。如果直到M都不存在该数组,则打印-1

例子:

方法:主要方法是需要检查数组是否为空或已满,并且可以通过将匝数除以数组大小来查看。如果余数为0,则可以是以下任一情况(空或填充)。
通过观察,可以看出该数组在奇数转弯中减小,而在偶数转弯中,该数组扩展,并且使用该观察结果,将检查M是否在索引之外或在数组内部。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to perform the queries
void PerformQueries(vector& a,
                    vector >& vec)
{
  
    vector ans;
  
    // Size of the array with
    // 1-based indexing
    int n = (int)a.size() - 1;
  
    // Number of queries
    int q = (int)vec.size();
  
    // Iterating through the queries
    for (int i = 0; i < q; ++i) {
  
        long long t = vec[i].first;
        int m = vec[i].second;
  
        // If m is more than the
        // size of the array
        if (m > n) {
            ans.push_back(-1);
            continue;
        }
  
        // Count of turns
        int turn = t / n;
  
        // Find the remainder
        int rem = t % n;
  
        // If the remainder is 0 and turn is
        // odd then the array is empty
        if (rem == 0 and turn % 2 == 1) {
            ans.push_back(-1);
            continue;
        }
  
        // If the remainder is 0 and turn is
        // even then array is full and
        // is in its initial state
        if (rem == 0 and turn % 2 == 0) {
            ans.push_back(a[m]);
            continue;
        }
  
        // If the remainder is not 0
        // and the turn is even
        if (turn % 2 == 0) {
  
            // Current size of the array
            int cursize = n - rem;
  
            if (cursize < m) {
                ans.push_back(-1);
                continue;
            }
            ans.push_back(a[m + rem]);
        }
        else {
  
            // Current size of the array
            int cursize = rem;
            if (cursize < m) {
                ans.push_back(-1);
                continue;
            }
            ans.push_back(a[m]);
        }
    }
  
    // Print the result
    for (int i : ans)
        cout << i << "\n";
}
  
// Driver code
int main()
{
  
    // The intial array, -1 is for
    // 1 base indexing
    vector a = { -1, 1, 2, 3, 4, 5 };
  
    // Queries in the form of the pairs of (t, M)
    vector > vec = {
        { 1, 4 },
        { 6, 1 },
        { 3, 5 }
    };
  
    PerformQueries(a, vec);
  
    return 0;
}


Java
// Java implementation of the approach
  
import java.util.*;
  
class GFG
{
  
    // Function to perform the queries
    static void PerformQueries(int[] a, int[][] vec) 
    {
  
        Vector ans = new Vector<>();
  
        // Size of the array with
        // 1-based indexing
        int n = (int) a.length - 1;
  
        // Number of queries
        int q = (int) vec.length;
  
        // Iterating through the queries
        for (int i = 0; i < q; ++i) 
        {
  
            long t = vec[i][0];
            int m = vec[i][1];
  
            // If m is more than the
            // size of the array
            if (m > n)
            {
                ans.add(-1);
                continue;
            }
  
            // Count of turns
            int turn = (int) (t / n);
  
            // Find the remainder
            int rem = (int) (t % n);
  
            // If the remainder is 0 and turn is
            // odd then the array is empty
            if (rem == 0 && turn % 2 == 1) 
            {
                ans.add(-1);
                continue;
            }
  
            // If the remainder is 0 and turn is
            // even then array is full and
            // is in its initial state
            if (rem == 0 && turn % 2 == 0) 
            {
                ans.add(a[m]);
                continue;
            }
  
            // If the remainder is not 0
            // and the turn is even
            if (turn % 2 == 0) 
            {
  
                // Current size of the array
                int cursize = n - rem;
  
                if (cursize < m)
                {
                    ans.add(-1);
                    continue;
                }
                ans.add(a[m + rem]);
            } 
            else 
            {
  
                // Current size of the array
                int cursize = rem;
                if (cursize < m)
                {
                    ans.add(-1);
                    continue;
                }
                ans.add(a[m]);
            }
        }
  
        // Print the result
        for (int i : ans)
            System.out.print(i + "\n");
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        // The intial array, -1 is for
        // 1 base indexing
        int[] a = { -1, 1, 2, 3, 4, 5 };
  
        // Queries in the form of the pairs of (t, M)
        int[][] vec = { { 1, 4 }, { 6, 1 }, { 3, 5 } };
  
        PerformQueries(a, vec);
  
    }
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to perform the queries 
def PerformQueries(a, vec) : 
  
    ans = []; 
  
    # Size of the array with 
    # 1-based indexing 
    n = len(a) - 1; 
  
    # Number of queries 
    q = len(vec); 
  
    # Iterating through the queries 
    for i in range(q) :
  
        t = vec[i][0]; 
        m = vec[i][1]; 
  
        # If m is more than the 
        # size of the array 
        if (m > n) :
            ans.append(-1); 
            continue; 
  
        # Count of turns 
        turn = t // n; 
  
        # Find the remainder 
        rem = t % n; 
  
        # If the remainder is 0 and turn is 
        # odd then the array is empty 
        if (rem == 0 and turn % 2 == 1) :
            ans.append(-1); 
            continue; 
  
        # If the remainder is 0 and turn is 
        # even then array is full and 
        # is in its initial state 
        if (rem == 0 and turn % 2 == 0) :
            ans.append(a[m]); 
            continue; 
  
        # If the remainder is not 0 
        # and the turn is even 
        if (turn % 2 == 0) :
  
            # Current size of the array 
            cursize = n - rem; 
  
            if (cursize < m) :
                ans.append(-1); 
                continue; 
      
            ans.append(a[m + rem]);
              
        else :
  
            # Current size of the array 
            cursize = rem; 
              
            if (cursize < m) :
                ans.append(-1); 
                continue; 
          
            ans.append(a[m]); 
  
    # Print the result 
    for i in ans :
        print(i) ; 
  
# Driver code 
if __name__ == "__main__" : 
  
    # The intial array, -1 is for 
    # 1 base indexing 
    a = [ -1, 1, 2, 3, 4, 5 ]; 
  
    # Queries in the form of the pairs of (t, M) 
    vec = [ 
        [ 1, 4 ], 
        [ 6, 1 ], 
        [ 3, 5 ] 
    ]; 
  
    PerformQueries(a, vec); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
    // Function to perform the queries
    static void PerformQueries(int[] a, int[,] vec) 
    {
  
        List ans = new List();
  
        // Size of the array with
        // 1-based indexing
        int n = (int) a.Length - 1;
  
        // Number of queries
        int q = (int) vec.GetLength(0);
  
        // Iterating through the queries
        for (int i = 0; i < q; ++i) 
        {
  
            long t = vec[i, 0];
            int m = vec[i, 1];
  
            // If m is more than the
            // size of the array
            if (m > n)
            {
                ans.Add(-1);
                continue;
            }
  
            // Count of turns
            int turn = (int) (t / n);
  
            // Find the remainder
            int rem = (int) (t % n);
  
            // If the remainder is 0 and turn is
            // odd then the array is empty
            if (rem == 0 && turn % 2 == 1) 
            {
                ans.Add(-1);
                continue;
            }
  
            // If the remainder is 0 and turn is
            // even then array is full and
            // is in its initial state
            if (rem == 0 && turn % 2 == 0) 
            {
                ans.Add(a[m]);
                continue;
            }
  
            // If the remainder is not 0
            // and the turn is even
            if (turn % 2 == 0) 
            {
  
                // Current size of the array
                int cursize = n - rem;
  
                if (cursize < m)
                {
                    ans.Add(-1);
                    continue;
                }
                ans.Add(a[m + rem]);
            } 
            else
            {
  
                // Current size of the array
                int cursize = rem;
                if (cursize < m)
                {
                    ans.Add(-1);
                    continue;
                }
                ans.Add(a[m]);
            }
        }
  
        // Print the result
        foreach (int i in ans)
            Console.Write(i + "\n");
    }
  
    // Driver code
    public static void Main(String[] args)
    {
  
        // The intial array, -1 is for
        // 1 base indexing
        int[] a = { -1, 1, 2, 3, 4, 5 };
  
        // Queries in the form of the pairs of (t, M)
        int[,] vec = { { 1, 4 }, { 6, 1 }, { 3, 5 } };
  
        PerformQueries(a, vec);
  
    }
}
  
// This code is contributed by 29AjayKumar


输出:
5
1
-1