73282024-01-07 17:19:01czitaSípálya (55 pont)csharpTime limit exceeded 21/55501ms27512 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sipalyak
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] st = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = st[0];
            int K = st[1];
            int[] kszum = new int[K + 1];
            for (int i = 1; i < K + 1; i++)
            {
                kszum[i] =kszum[i-1]+i;
            }
            //  int[] P = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int[] P = new int[N];
            string[] t = Console.ReadLine().Split();
            for (int i = 0; i < N; i++)
            {
                P[i] = int.Parse(t[i]);
            }
            //int[] P = { 980855446, 998323020, 997418759, 994832636, 972080651, 973844772, 982446881, 999529807, 994574431, 998939886, 993768883, 999361467, 985621890, 994479633, 992913410, 994225250, 988670181, 992335377, 991835063, 997798360, 993513355, 988058282, 975548123, 999501819, 996917832, 994805932, 984880431, 999542936, 992620889, 996123248 };
            long max = 0, maxi = 0, szum = 0, minepit, max2 = 0, maxi2 = 0;
            for (int i = 0; i < K; i++)
            {
                if (max <= P[i])
                {
                    maxi = i;
                    max = P[i];
                    max2 = max;
                    maxi2 = i;
                }
                else
                {
                    max2--;
                    maxi2++;
                    if (max2 < P[i])
                    {
                        max2 = P[i];
                        maxi2 = i;
                    }
                }
                szum += P[i];
            }
            if (maxi2 - maxi > max - max2)
                minepit = K * max2 - szum + kszum[maxi2  ] - kszum[K - maxi2 - 1];
            else
               minepit = K * max - szum + kszum[maxi ] - kszum[K - maxi - 1];

            long aktepit = 0;
            for (int i = K; i < N; i++)
            {
                szum -= P[i - K];
                szum += P[i];
                if (P[i] >= max)
                {
                    maxi = i;
                    max = P[i];
                    max2 = max;
                    maxi2 = i;
                }
                else
                {
                    max = P[i - K + 1];
                    max2 = P[i-K+1];
                    maxi2 = i - K + 1;
                    maxi = maxi2;
                    for (int j = i-K+1; j < i+1; j++)
                    {
                        if (max <= P[j])
                        {
                            maxi = j;
                            max = P[j];
                            max2 = max;
                            maxi2 = j;
                        }
                        else
                        {
                            max2--;
                            maxi2++;
                            if (max2 < P[j])
                            {
                                max2 = P[j];
                                maxi2 = j;
                            }
                        }
                    }
                    
                }
                if (maxi2 - maxi > max - max2)
                    aktepit = K * max2 - szum + kszum[maxi2 - (i-K+1)] - kszum[i - maxi2];
                else
                    aktepit = K * max - szum + kszum[maxi - (i-K+1) ] - kszum[i - maxi];
                if (minepit > aktepit)
                {
                    minepit = aktepit;
                }
            }
            Console.WriteLine(minepit);
            Console.ReadKey();
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base21/55
1Accepted0/032ms22248 KiB
2Accepted0/032ms22704 KiB
3Accepted2/237ms24060 KiB
4Accepted2/237ms24340 KiB
5Accepted2/237ms24492 KiB
6Accepted2/237ms24528 KiB
7Accepted3/337ms24892 KiB
8Accepted1/164ms26296 KiB
9Accepted1/171ms26344 KiB
10Accepted1/178ms26184 KiB
11Accepted1/182ms26904 KiB
12Accepted1/181ms26740 KiB
13Accepted1/1135ms27092 KiB
14Accepted2/2144ms27512 KiB
15Accepted2/281ms27356 KiB
16Time limit exceeded0/2439ms25964 KiB
17Time limit exceeded0/2481ms26084 KiB
18Time limit exceeded0/2469ms26464 KiB
19Time limit exceeded0/3474ms26480 KiB
20Time limit exceeded0/2501ms26268 KiB
21Time limit exceeded0/2477ms26168 KiB
22Time limit exceeded0/2465ms26336 KiB
23Time limit exceeded0/2469ms26664 KiB
24Time limit exceeded0/2458ms27232 KiB
25Time limit exceeded0/2456ms26864 KiB
26Time limit exceeded0/2453ms26988 KiB
27Time limit exceeded0/2486ms26948 KiB
28Time limit exceeded0/3469ms27196 KiB
29Time limit exceeded0/3449ms27244 KiB
30Time limit exceeded0/3462ms27140 KiB