TonyTroev

DynamicList

Feb 13th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. /*
  2.  Create a list, seqList , of N empty sequences, where each sequence is indexed from 0 to N - 1. The elements within each of the N sequences also use 0-indexing.
  3. Create an integer, lastAnswer, and initialize it to 0.
  4. The  types of queries that can be performed on your list of sequences () are described below:
  5. Query: 1 x y
  6. Find the sequence, seq, at index (x ^ lastAnswer) % N in seqList.
  7. Append integer y to sequence seq.
  8. Query: 2 x y
  9. Find the sequence, seq, at index (x ^ lastAnswer) % N in seqList.
  10. Find the value of element y % size in seq (where size is the size of seq) and assign it to lastAnswer.
  11. Print the new value of lastAnswer on a new line*/
  12.  
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16.  
  17. namespace _54_DynamicArray
  18. {
  19.     class DynamicArray
  20.     {
  21.         static void Main(string[] args)
  22.         {
  23.             int[] initialData = Console.ReadLine()
  24.                 .Split(' ')
  25.                 .Select(int.Parse)
  26.                 .ToArray();
  27.             List<List<int>> seqList = new List<List<int>>(initialData[0]);
  28.             for (int i = 0; i < initialData[0]; i++)
  29.             {
  30.                 seqList.Add(new List<int>());
  31.             }
  32.  
  33.             int lastAnswer = 0;
  34.             for (int i = 0; i < initialData[1]; i++)
  35.             {
  36.                 int[] data = Console.ReadLine()
  37.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  38.                 .Select(int.Parse)
  39.                 .ToArray();
  40.  
  41.                 if (data[0] == 1)
  42.                 {
  43.                     seqList[(data[1] ^ lastAnswer) % seqList.Count].Add(data[2]);
  44.                 }
  45.                 else
  46.                 {
  47.                     lastAnswer = seqList[(data[1] ^ lastAnswer) % seqList.Count][data[2] % seqList[(data[1] ^ lastAnswer) % seqList.Count].Count];
  48.                     Console.WriteLine(lastAnswer);
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment