Example #1
0
        public TAnswer SumsArray(TSample Sample)
        {
            MaximumSubarraySumSample sample = Sample as MaximumSubarraySumSample;

            Int64[] arr = sample.arr;
            Int64   m   = sample.m;

            Int64[] sums = new Int64[arr.Length];

            Int64 result = 0;

            Int64 temp = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i]  = arr[i] % m;
                temp    = (arr[i] + temp) % m;
                sums[i] = temp;
                if (temp > result)
                {
                    result = temp;
                }
                if (arr[i] > result)
                {
                    result = arr[i];
                }
            }


            Int32[] idx = Enumerable.Range(0, sums.Length).ToArray();
            QS(sums, 0, sums.Length - 1, idx);

            for (int i = 0; i < sums.Length - 1; i++)
            {
                if (idx[i] > idx[i + 1])
                {
                    temp = (sums[i] - sums[i + 1] + m) % m;
                    if (temp > result)
                    {
                        result = temp;
                    }
                }
            }



            return(new MaximumSubarraySumAnswer()
            {
                result = result
            });
        }
Example #2
0
        public override void CreateSamples(StreamReader reader)
        {
            int q = Convert.ToInt32(reader.ReadLine());

            for (int qItr = 0; qItr < q; qItr++)
            {
                string[] nm = reader.ReadLine().Split(' ');

                int n = Convert.ToInt32(nm[0]);

                long m = Convert.ToInt64(nm[1]);

                long[] a = Array.ConvertAll(reader.ReadLine().Split(' '), aTemp => Convert.ToInt64(aTemp));
                MaximumSubarraySumSample sample = new MaximumSubarraySumSample()
                {
                    arr = a, m = m
                };
                Samples.Add(sample);
            }
        }
Example #3
0
        //[SolutionMethod]
        public TAnswer OpDP(TSample Sample)
        {
            MaximumSubarraySumSample sample = Sample as MaximumSubarraySumSample;

            Int64[] arr = sample.arr;
            Int64   m   = sample.m;

            Int32 nc   = 0;
            Int64 maxm = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                arr[nc] = arr[i] % m;
                if (arr[nc] != 0)
                {
                    if (maxm < arr[nc])
                    {
                        maxm = arr[nc];
                    }
                    nc++;
                }
            }

            Int64 result = maxm;


            if (result != m - 1)
            {
                Int64[] v  = new Int64[arr.Length];
                Int32   vc = 1;
                v[0] = arr[0];


                for (int i = 1; i < nc; i++)
                {
                    Int32 vptr = -1;
                    for (int j = 0; j < vc; j++)
                    {
                        Int64 ts = v[j] + arr[i];
                        if (ts == m)
                        {
                            continue;
                        }
                        if (ts > m)
                        {
                            ts = ts - m;
                        }

                        vptr++;
                        v[vptr] = ts;
                        if (v[vptr] > result)
                        {
                            result = v[vptr];
                        }
                    }
                    vptr++;
                    v[vptr] = arr[i];
                    vc      = vptr + 1;
                }

                Int64[] vcd = v.Take(vc).Distinct().ToArray();
                Console.WriteLine($"vc = {vc} vcd count = {vcd.Count()}");
            }

            return(new MaximumSubarraySumAnswer()
            {
                result = result
            });
        }