73272024-01-07 17:03:25czitaSípálya (55 pont)csharpTime limit exceeded 21/55490ms27644 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 = { 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/030ms22424 KiB
2Accepted0/032ms23016 KiB
3Accepted2/237ms23832 KiB
4Accepted2/235ms23752 KiB
5Accepted2/237ms24268 KiB
6Accepted2/237ms24460 KiB
7Accepted3/337ms24736 KiB
8Accepted1/164ms26404 KiB
9Accepted1/171ms26524 KiB
10Accepted1/178ms27020 KiB
11Accepted1/186ms27020 KiB
12Accepted1/182ms27248 KiB
13Accepted1/1143ms27416 KiB
14Accepted2/2159ms27616 KiB
15Accepted2/286ms27576 KiB
16Time limit exceeded0/2460ms25480 KiB
17Time limit exceeded0/2474ms26616 KiB
18Time limit exceeded0/2490ms26324 KiB
19Time limit exceeded0/3477ms26764 KiB
20Time limit exceeded0/2437ms26592 KiB
21Time limit exceeded0/2465ms26500 KiB
22Time limit exceeded0/2465ms26964 KiB
23Time limit exceeded0/2465ms26716 KiB
24Time limit exceeded0/2462ms27644 KiB
25Time limit exceeded0/2486ms27084 KiB
26Time limit exceeded0/2458ms27304 KiB
27Time limit exceeded0/2476ms27316 KiB
28Time limit exceeded0/3462ms27324 KiB
29Time limit exceeded0/3462ms27400 KiB
30Time limit exceeded0/3486ms27392 KiB