UOJ Logo axuhongbo的博客

博客

NOIP模拟赛2 题解

2019-08-18 20:26:15 By axuhongbo

1.计算系数

移步洛谷

2.聪明的质检员

移步洛谷

C++ 代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL; const int N = 200010;

int n, m;
LL S;
int w[N], v[N];
int l[N], r[N];
int cnt[N];
LL sum[N];

LL get(int W)
{
    for (int i = 1; i <= n; i++)
        if (w[i] >= W)
        {
            sum[i] = sum[i - 1] + v[i];
            cnt[i] = cnt[i - 1] + 1;
        }
    else
    {
        sum[i] = sum[i - 1];
        cnt[i] = cnt[i - 1];
    }

    LL res = 0;
    for (int i = 0; i < m; i++) res += (cnt[r[i]] - cnt[l[i] - 1]) *(sum[r[i]] - sum[l[i] - 1]);
    return res;
}

int main()
{
    scanf("%d%d%lld", &n, &m, &S);
    for (int i = 1; i <= n; i++) scanf("%d%d", &w[i], &v[i]);
    for (int i = 0; i < m; i++) scanf("%d%d", &l[i], &r[i]);

    int l = 0, r = 1e6 + 1;
    while (l < r)
    {
        int mid = l + r + 1 >> 1;
        if (get(mid) >= S) l = mid;
        else r = mid - 1;
    }

    printf("%lld\n", min(abs(get(r) - S), abs(S - get(r + 1))));

    return 0;
}

3.观光公交

移步洛谷

#include <iostream>
#include <algorithm>
using namespace std; const int N = 1010,
    M = 10010;

int n, m, k;
int d[N];
int t[M], a[M], b[M];
int tm[N], last[N];
int sum[N];
int reduce[N];

int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i < n; i++) scanf("%d", &d[i]);
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d%d", &t[i], &a[i], &b[i]);
        last[a[i]] = max(last[a[i]], t[i]);
        sum[b[i]]++;
    }

    for (int i = 1; i <= n; i++) tm[i] = max(tm[i - 1], last[i - 1]) + d[i - 1];

    while (k--)
    {
        for (int i = n; i >= 2; i--)
            if (!d[i - 1]) reduce[i - 1] = 0;
            else
            {
                reduce[i - 1] = sum[i];
                if (tm[i] > last[i]) reduce[i - 1] += reduce[i];
            }

        int p = 0;
        for (int i = 1; i < n; i++)
            if (reduce[p] < reduce[i])
                p = i;

        if (!p) break;
        d[p]--;
        for (int i = p + 1; i <= n; i++) tm[i] = max(tm[i - 1], last[i - 1]) + d[i - 1];
    }

    int res = 0;
    for (int i = 0; i < m; i++) res += tm[b[i]] - t[i];

    printf("%d\n", res);
    return 0;
}

评论

terran
@啊绿博

发表评论

可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。