QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607873#5507. Investorshzy99999#RE 0ms0kbC++202.1kb2024-10-03 16:49:102024-10-03 16:49:12

Judging History

你现在查看的是最新测评结果

  • [2024-10-03 16:49:12]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-03 16:49:10]
  • 提交

answer

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 6010;
int T;
int n, m;
int tr1[N], tr2[N];
int a[N], c[N];
int lowbit(int x)
{
    return x & -x;
}
int add(int tr[], int x, int c)
{
    for (int i = x; i <= n; i += lowbit(i))
        tr[i] += c;
}
int find(int tr[], int x)
{
    int res = 0;
    for (int i = x; i > 0; i -= lowbit(i))
        res += tr[i];
    return res;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            cin >> a[i], c[i] = 0, tr1[i] = tr2[i] = 0;
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            int cnt = 0;
            for (int j = 1; j < i; j++)
                if (a[j] > a[i])
                    cnt++;
            c[i] = cnt;
            ans += cnt;
            // cout << c[i] << ' ';
        }
        // cout << endl;

        for (int k = 1; k <= m; k++)
        {
            for (int i = 1; i <= n; i++)
                tr1[i] = tr2[i] = 0;
            int pos = 0, maxv = 0;
            for (int i = n; i; i--)
            {
                if (!c[i])
                    continue;
                add(tr1, c[i], c[i]);
                add(tr2, c[i], 1);
                int res1 = find(tr1, c[i] - 1);
                int res2 = (find(tr2, n) - find(tr2, c[i] - 1)) * c[i];
                if (!pos || maxv < res1 + res2)
                    pos = i, maxv = res1 + res2;
                // cout << ":" << i << ' ' << res1 << ' ' << res2 << ' ' << maxv << endl;
            }
            if (!maxv)
                break;
            ans -= maxv;
            int t = c[pos];
            for (int i = pos; i <= n; i++)
                c[i] = max(0, c[i] - t);
        }
        cout << ans << endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

2
6 1
4 5 6 2 2 1
6 2
4 5 6 2 2 1

output:


result: