codeforces 974div3C题被System test卡了
  • 板块题目总版
  • 楼主MisakaW
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/9/22 15:00
  • 上次更新2024/9/22 16:58:21
查看原帖
codeforces 974div3C题被System test卡了
988102
MisakaW楼主2024/9/22 15:00

小萌新刚学算法,初试炼了codeforces,974div3只做出了A、B、C、D(哭),当时是AC了。结果第二天上来发现B、C、D题都红了(大哭)。 (好吧,过了一会儿,发现B题变回绿色了)

我的代码


C. Robin Hood in Town

codeforces 974 div3链接

想问一下,这道题是因为直接用sort偷懒的原因吗?还有其它什么问题? (我最后没有用写的msort快速选择函数,因为第五个测试点会超时。)

#include <bits/stdc++.h>
#define let auto &
#define var auto
#define in :
#define to0(a) memset(a, 0, sizeof(a))
#define to1(a) memset(a, -1, sizeof(a))
#define N 200005

using namespace std;

using ll = long long;
using cint = const int;
cint inf = INT_MAX;
long long n;
int arr[N];
void msort(int l, int r, int m) // 写的不对,不用了
{
    int mid = m, i = l, p = arr[mid];
    swap(arr[mid], arr[r]);
    for (int j = l; j < r; ++j)
    {
        if (arr[j] < p)
        {
            swap(arr[i], arr[j]);
            ++i;
        }
    }
    swap(arr[i], arr[r]);
    if (i == m)
        return;
    else if (i > m)
        msort(l, i - 1, m);
    else
        msort(i + 1, r, m);
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t;
    ll sum = 0ll;
    cin >> t;
    while (t--)
    {
        cin >> n;
        int mid = (n >> 1);
        sum = 0ll;
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            sum += arr[i];
        }
        if (n <= 2)
        {
            cout << -1 << endl;
            continue;
        }
        sort(arr, arr + n);
        // msort(0, n - 1, mid);
        mid = arr[mid];

        if (mid * n * 2 < sum)
        {
            cout << 0 << endl;
        }
        else
        {
            cout << ((n * mid) * 2) - sum + 1ll << endl;
        }
    }

    return 0;
}

D.Robert Hood and Mrs Hood

这题还在被测试中,不知道会不会被卡。请多多指教!

#include <bits/stdc++.h>
#define let auto &
#define var auto
#define in :
#define to0(a) memset(a, 0, sizeof(a))
#define to1(a) memset(a, -1, sizeof(a))
#define N 200005

using namespace std;

using ll = long long;
using cint = const int;
cint inf = INT_MAX;
typedef pair<int, int> pp;
long long n, d, k;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t, a, b;
    cin >> t;
    while (t--)
    {
        cin >> n >> d >> k;
        vector<pp> arr;
        for (int i = 0; i < k; i++)
        {
            cin >> a >> b;
            arr.push_back({a, b});
        }
        sort(arr.begin(), arr.end(), less<pp>());
        priority_queue<pp, vector<pp>, greater<pp>> q;
        int i = 0;
        vector<ll> works(n + 1);
        int mom = d, bro = d;
        for (int day = 1; day <= n; day++)
        {
            works[day] = works[day - 1];
            while (i < k && arr[i].first <= day)
            {
                ++works[day];
                q.push({arr[i].second, arr[i].first});
                ++i;
            }
            while (!q.empty() && q.top().first <= day - d)
            {
                --works[day];
                q.pop();
            }
            // cout << "day" << day << ':' << works[day] << endl;
        }

        for (int i = d; i <= n; i++)
        {
            if (works[i] < works[mom])
            {
                mom = i;
            }
            else if (works[i] > works[bro])
            {
                bro = i;
            }
        }
        // cout << "res:";
        cout << bro - d + 1 << ' ' << mom - d + 1 << endl;
        // cout << endl;
    }

    return 0;
}
2024/9/22 15:00
加载中...