Submission #32937679


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks
{
    public class E
    {
        public static void Main()
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var (N, Q, X) = Scanner.Scan<int, int, long>();
            var W = Scanner.ScanEnumerable<long>().ToArray();
            var sum = W.Sum();

            var cumW = new long[N * 2 + 1];
            for (var i = 0; i < N * 2; i++)
            {
                cumW[i + 1] = cumW[i] + W[i % N];
            }

            var counts = new long[N];
            {
                var r = 0;
                var x = X % sum;
                for (var l = 0; l < N; l++)
                {
                    counts[l] = X / sum * N;
                    while (cumW[r] - cumW[l] < x) r++;
                    counts[l] += r - l;
                }
            }

            var next = new int[N];
            for (var i = 0; i < N; i++)
            {
                next[i] = (int)((i + counts[i]) % N);
            }

            var steps = new List<int>();
            var dict = new Dictionary<int, int>();
            var noLoopLength = 0;
            var loopLength = 0;
            {
                var curr = 0;
                for (var i = 0; ; i++)
                {
                    if (dict.ContainsKey(curr))
                    {
                        noLoopLength = dict[curr];
                        loopLength = i - dict[curr];
                        break;
                    }

                    dict[curr] = i;
                    steps.Add(curr);
                    curr = next[curr];
                }
            }

            while (Q-- > 0)
            {
                var K = Scanner.Scan<long>() - 1;
                if (K <= noLoopLength)
                {
                    var box = steps[(int)K];
                    Console.WriteLine(counts[box]);
                }
                else
                {
                    var mod = (K - noLoopLength) % loopLength;
                    var box = steps[(int)(noLoopLength + mod)];
                    Console.WriteLine(counts[box]);
                }
            }
        }

        public static class Scanner
        {
            public static string ScanLine() => Console.ReadLine()?.Trim() ?? string.Empty;
            public static string[] Scan() => ScanLine().Split(' ');
            public static T Scan<T>() where T : IConvertible => Convert<T>(Scan()[0]);
            public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]));
            }
            public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]));
            }
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]));
            }
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]), Convert<T5>(line[4]));
            }
            public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
            {
                var line = Scan();
                return (Convert<T1>(line[0]), Convert<T2>(line[1]), Convert<T3>(line[2]), Convert<T4>(line[3]), Convert<T5>(line[4]), Convert<T6>(line[5]));
            }
            public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => Scan().Select(Convert<T>);
            private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
        }
    }
}

Submission Info

Submission Time
Task E - Packing Potatoes
User AconCavy
Language C# (.NET Core 3.1.201)
Score 500
Code Size 4801 Byte
Status AC
Exec Time 232 ms
Memory 76132 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 22
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt
Case Name Status Exec Time Memory
example_00.txt AC 92 ms 28200 KiB
example_01.txt AC 84 ms 27952 KiB
test_00.txt AC 226 ms 60428 KiB
test_01.txt AC 196 ms 57104 KiB
test_02.txt AC 170 ms 53564 KiB
test_03.txt AC 133 ms 44052 KiB
test_04.txt AC 143 ms 43444 KiB
test_05.txt AC 232 ms 61992 KiB
test_06.txt AC 152 ms 48532 KiB
test_07.txt AC 189 ms 59160 KiB
test_08.txt AC 141 ms 55508 KiB
test_09.txt AC 146 ms 44508 KiB
test_10.txt AC 215 ms 58692 KiB
test_11.txt AC 131 ms 42040 KiB
test_12.txt AC 105 ms 34804 KiB
test_13.txt AC 177 ms 45988 KiB
test_14.txt AC 163 ms 44172 KiB
test_15.txt AC 203 ms 65212 KiB
test_16.txt AC 212 ms 60696 KiB
test_17.txt AC 213 ms 61020 KiB
test_18.txt AC 219 ms 76132 KiB
test_19.txt AC 210 ms 58516 KiB