Statistics

Problem Statement for "DejaVu"

Problem Statement

Misof recently had an accident in which he managed to cut his left hand on some broken glass. He is now "all right" - meaning that he can only use his right hand for a while. Help him with some issues he has.

Misof is lying at home and he is bored. He would like to watch a movie in which he can enjoy the sense of a "deja vu" - that is, seeing the same scene for a second time.

For the purpose of this problem, a movie is a sequence of scenes, and we will use nonnegative integers to describe them. Two scenes are different iff they have a different number.

A "deja vu" is a scene that occurs exactly twice in the movie. (A scene that occurs more than two times does not count.)

Misof has already selected a movie to watch, but then he came up with an interesting observation: what if he could increase the number of deja vus by only watching some part of the movie?

Formally, for a sequence X of integers let dejavu(X) be the number of elements that occur in X exactly twice. You are given a sequence M. Find a contiguous subsequence M' of M that maximizes dejavu(M') and return the value dejavu(M').



The sequence M of length N is constructed using the following pseudocode:

A[0] = seed
for i = 1 to N-1:
    A[i] = (A[i-1] * 1664525 + 1013904223) modulo 4294967296
for i = 0 to N-1:
    M[i] = A[i] modulo R

Definition

Class:
DejaVu
Method:
mostDejaVus
Parameters:
int, int, int
Returns:
int
Method signature:
int mostDejaVus(int N, int seed, int R)
(be sure your method is public)

Notes

  • The reference solution would correctly solve any sequence of length N, it does not depend on the properties of the pseudorandom generator.
  • Watch out for integer overflow when generating M.

Constraints

  • N will be between 1 and 200,000, inclusive.
  • seed will be between 0 and 10^9, inclusive.
  • R will be between 1 and 10^9, inclusive.

Examples

  1. 10

    47

    5

    Returns: 2

    A = { 47, 1092136898, 2326342713, 3532868, 1750783699, 3040800278, 3282292349, 2587573688, 3003147703, 1792673706 } M = { 2, 3, 3, 3, 4, 3, 4, 3, 3, 1 } The contiguous subsequences {3, 4, 3, 4} and {4, 3, 4, 3} contain two deja vus each, and this is the best Misof can get.

  2. 14

    474747

    7

    Returns: 3

    M = { 0, 2, 2, 1, 0, 0, 6, 0, 6, 0, 2, 1, 1, 0 } The optimal solutions are the subsequences M[2:12] = {2, 1, 0, 0, 6, 0, 6, 0, 2, 1} and M[6:13] = {6, 0, 6, 0, 2, 1, 1}. Each of these contains three deja vus. Note that for M[2:12] the scene 0 does not count as a deja vu, as it occurs too many times. Only scenes 2, 1, and 6 give Misof one valid deja vu each.

  3. 1

    47

    100

    Returns: 0

  4. 2

    47

    1

    Returns: 1

  5. 2

    47

    100000

    Returns: 0

  6. 146117

    67135896

    694466506

    Returns: 14

  7. 159463

    612881716

    43741

    Returns: 12131

  8. 167167

    415062492

    760953937

    Returns: 18

  9. 105361

    853713569

    807195153

    Returns: 3

  10. 103101

    110011131

    9

    Returns: 9

  11. 163118

    777236130

    11

    Returns: 10

  12. 174392

    459238762

    343

    Returns: 130

  13. 168585

    551852547

    67962

    Returns: 18456

  14. 131725

    35717065

    56327

    Returns: 15250

  15. 178771

    367127108

    344

    Returns: 133

  16. 195571

    982424200

    5

    Returns: 5

  17. 143768

    105169864

    85805

    Returns: 22492

  18. 150276

    545789166

    22726

    Returns: 6361

  19. 185040

    936243973

    815

    Returns: 278

  20. 143890

    234670474

    899056097

    Returns: 11

  21. 114437

    830082628

    749243544

    Returns: 5

  22. 146274

    724619612

    41323

    Returns: 11354

  23. 172779

    989998540

    56785

    Returns: 15581

  24. 169820

    607076112

    16

    Returns: 16

  25. 140921

    604235495

    379434604

    Returns: 22

  26. 195710

    557700496

    720

    Returns: 251

  27. 114933

    673673332

    488

    Returns: 174

  28. 163622

    271805617

    554

    Returns: 193

  29. 101991

    182853533

    3

    Returns: 3

  30. 134832

    548934239

    940026799

    Returns: 6

  31. 157096

    392537040

    20

    Returns: 17

  32. 134735

    965501521

    860408746

    Returns: 8

  33. 183666

    857357003

    12914

    Returns: 3629

  34. 175618

    260940257

    454

    Returns: 166

  35. 156575

    930735750

    280

    Returns: 112

  36. 161576

    988878479

    230

    Returns: 93

  37. 112899

    146737412

    79127

    Returns: 19318

  38. 151427

    116055547

    68185

    Returns: 18713

  39. 100059

    700751158

    14

    Returns: 13

  40. 108803

    396477633

    9

    Returns: 9

  41. 105466

    150794607

    856

    Returns: 288

  42. 167064

    897188377

    315981477

    Returns: 38

  43. 164840

    567230838

    705166843

    Returns: 16

  44. 174361

    10723827

    6

    Returns: 6

  45. 119735

    989836379

    93299

    Returns: 21335

  46. 106119

    942251502

    829

    Returns: 270

  47. 108026

    406657323

    1

    Returns: 1

  48. 111611

    325052044

    18

    Returns: 15

  49. 140326

    256750958

    663

    Returns: 226

  50. 175990

    818650486

    91131

    Returns: 24571

  51. 174820

    86654360

    467988208

    Returns: 23

  52. 172944

    56030499

    14

    Returns: 13

  53. 154741

    379828288

    83966

    Returns: 22639

  54. 199578

    525333703

    165

    Returns: 74

  55. 166568

    125699083

    801102584

    Returns: 14

  56. 159476

    884403724

    14

    Returns: 13

  57. 173915

    560627570

    10

    Returns: 10

  58. 192373

    526350671

    8

    Returns: 8

  59. 146162

    854968508

    31878

    Returns: 8788

  60. 160628

    24260175

    15

    Returns: 13

  61. 160362

    297197665

    18

    Returns: 14

  62. 124810

    884873256

    15359

    Returns: 4281

  63. 200000

    12331214

    2

    Returns: 2

  64. 200000

    10

    10000000

    Returns: 1920


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: