Пример #1
0
    void Calc()
    {
        int N = re.i();

        int[] white = new int[N];
        int[] black = new int[N];
        for (int i = 0; i < 2 * N; i++)
        {
            if (re.s() == "W")
            {
                white[re.i() - 1] = i;
            }
            else
            {
                black[re.i() - 1] = i;
            }
        }
        long[,] DP = new long[N + 1, N + 1];
        for (int i = 0; i <= N; i++)
        {
            SegTree Seg = new SegTree(2 * N, this);
            for (int j = 0; j < i; j++)
            {
                Seg.Add(white[j]);
            }
            for (int j = 0; j <= N; j++)
            {
                if (i == 0 & j == 0)
                {
                    DP[i, j] = 0;
                }
                else if (i == 0)
                {
                    DP[i, j] = DP[i, j - 1] + Seg.Sum(black[j - 1] + 1, 2 * N - 1);
                    Seg.Add(black[j - 1]);
                }
                else if (j == 0)
                {
                    DP[i, j] = DP[i - 1, j] + Seg.Sum(white[i - 1] + 1, 2 * N - 1);
                }
                else
                {
                    Seg.Add(black[j - 1]);
                    DP[i, j] = Math.Min(DP[i - 1, j] + Seg.Sum(white[i - 1] + 1, 2 * N - 1), DP[i, j - 1] + Seg.Sum(black[j - 1] + 1, 2 * N - 1));
                }
            }
        }
        sb.Append(DP[N, N] + "\n");
    }
Пример #2
0
    void Calc()
    {
        string[] str = Console.ReadLine().Split(' ');
        int      N   = int.Parse(str[0]);
        long     K   = int.Parse(str[1]);

        long[] A = new long[N];
        for (int i = 0; i < N; i++)
        {
            A[i] = int.Parse(Console.ReadLine()) - K;
        }
        N++;
        long[] sum = new long[N];
        for (int i = 1; i < N; i++)
        {
            sum[i] = sum[i - 1] + A[i - 1];
        }
        long[] B = new long[N];
        int[]  C = new int[N];
        for (int i = 0; i < N; i++)
        {
            B[i] = sum[i];
            C[i] = i;
        }
        Array.Sort(C, (x, y) => (B[x] > B[y] ? 1 : (B[x] == B[y] ? (x > y ? 1 : (x == y ? 0 : -1)) : -1)));
        SegTree Seg   = new SegTree(N, this);
        long    count = 0;

        for (int i = N - 1; i >= 0; i--)
        {
            count += Seg.Sum(C[i], N - 1);
            Seg.Add(C[i]);
        }
        sb.Append(count + "\n");
    }
Пример #3
0
    void Calc()
    {
        string S = re.s();
        int    N = S.Length;

        int[] C = new int[26];
        for (int i = 0; i < N; i++)
        {
            C[S[i] - 'a']++;
        }
        int odd = 0;

        for (int i = 0; i < 26; i++)
        {
            if (C[i] % 2 == 1)
            {
                odd++;
            }
        }
        if (odd > 1 || (odd == 1 && N % 2 == 0))
        {
            sb.Append(-1 + "\n");
            return;
        }
        List <int>[] G = new List <int> [26];
        for (int i = 0; i < 26; i++)
        {
            G[i] = new List <int>();
        }
        int[] order = new int[N];
        for (int i = 0; i < N; i++)
        {
            order[i] = G[S[i] - 'a'].Count;
            G[S[i] - 'a'].Add(i);
        }
        SegTree Seg = new SegTree(N, this);

        for (int i = 2 * N - 2; i >= 0; i--)
        {
            if (i >= Seg.segf)
            {
                Seg.X[i] = 1;
            }
            else
            {
                int sl = i * 2 + 1;
                int sr = i * 2 + 2;
                Seg.X[i] = Seg.X[sl] + Seg.X[sr];
            }
        }
        long count  = 0;
        bool center = false;

        for (int i = 0; i < N; i++)
        {
            if (order[i] * 2 + 1 < C[S[i] - 'a'])
            {
                int pair = G[S[i] - 'a'][C[S[i] - 'a'] - 1 - order[i]];
                if (pair != N - 1)
                {
                    count += Seg.Sum(pair + 1, N - 1);
                }
                Seg.Delete(pair);
                if (center)
                {
                    count++;
                }
            }
            else if (order[i] * 2 + 1 == C[S[i] - 'a'])
            {
                center = true;
            }
        }
        sb.Append(count + "\n");
    }