7.11课堂题解

· · 题解

1.P13416 [COCI 2012/2013 #4] RAZLIKA

思路:

这道题没说要求的是连续的,所以我们可以先排序。然后以样例为例,排完序以后是:-3 -2 3 6 8然后因为题目说去掉两个数字,所以就相当于选出3个数字。

我们选几个,比如-2 3 6和-2 3 8。我们可以发现选的数字越紧凑,每个数之间的差值就越小,所以我们可以先排序,再在排序后的数组上进行单调栈。

代码:

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=1e6+5;
int n, k;
int a[N];
int d[N];
int q[N];
int ql=2, qr=1;
int ans=INT_MAX;
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n >> k;
    k=n-k;
    for(int i=1; i<=n; i++) cin >> a[i];
    sort(a+1, a+n+1);
    for(int i=2; i<=n; i++) d[i]=a[i]-a[i-1];
    for(int i=2; i<=n; i++)
    {
        while(qr>=ql && d[q[qr]]>=d[i]) qr--;
        while(qr>=ql && q[qr]+k<=i+1) ql++;
        q[++qr]=i;
        if(i>=k) ans=min(ans, a[i]-a[i-k+1]+d[q[ql]]);
    }
    cout << ans;
    return 0;
}

2.AT_abc137_d [ABC137D] Summer Vacation

思路:

我们可以用一个优先队列,枚举每一天,然后把可以在i天之内拿到报酬的工资存入优先队列,然后答案加上优先队列的最大值(队首)就完成了贪心。

代码:

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
struct p
{
    int t, w;
    bool operator<(const p &op) const
    {
        return t<op.t;
    }
};
const int N=1e5+5;
int n, m;
p a[N];
bool vis[N];
priority_queue<int> q;
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n >> m;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i].t >> a[i].w;
    }
    sort(a+1, a+n+1);
    int ans=0;
    int now=1;
    for(int i=1; i<=m; i++)
    {
        while(now<=n && a[now].t<=i)
        {
            q.push(a[now].w);
            now++;
        }
        if(q.size())
        {
            ans+=q.top();
            q.pop();
        }
    }
    cout << ans;
    return 0;
}

3.P9525 [JOIST 2022] 团队竞技 / Team Contest

思路:

贪心思路有点难想,代码其实很好写。

我们先建3个优先队列,分别存每个海狸的每个能力。弄个标记数组,表示这个海狸是否还能继续参与计算。

所以栈顶的元素能不能参与计算有两个条件:首先是队列首项不能被标记过,因为已经不能参与计算了;第二个是不能有两个及以上的队列首项存在它,表示他又两个及以上的能力值超过所有海狸了,标记后踢掉。

最后如果所有条件都满足,输出所有队列首项之和。

代码:

#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
struct p
{
    int x, bh;
    bool operator<(const p &op) const
    {
        return x<op.x;
    }
};
const int N=1e5+5e4+5;
int n;
int a[N], b[N], c[N];
priority_queue<p> A;
priority_queue<p> B;
priority_queue<p> C;
int vis[N];
signed main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        A.push(p{a[i], i});
        B.push(p{b[i], i});
        C.push(p{c[i], i});
    }
    while(A.size() && B.size() && C.size())
    {
        if(vis[A.top().bh])
        {
            A.pop();
            continue;
        }
        if(vis[B.top().bh])
        {
            B.pop();
            continue;
        }
        if(vis[C.top().bh])
        {
            C.pop();
            continue;
        }

        if(b[A.top().bh]==B.top().x)
        {
            vis[A.top().bh]=1;
            A.pop();
            continue;
        }
        if(c[A.top().bh]==C.top().x)
        {
            vis[A.top().bh]=1;
            A.pop();
            continue;
        }

        if(a[B.top().bh]==A.top().x)
        {
            vis[B.top().bh]=1;
            B.pop();
            continue;
        }
        if(c[B.top().bh]==C.top().x)
        {
            vis[B.top().bh]=1;
            B.pop();
            continue;
        }

        if(a[C.top().bh]==A.top().x)
        {
            vis[C.top().bh]=1;
            C.pop();
            continue;
        }
        if(b[C.top().bh]==B.top().x)
        {
            vis[C.top().bh]=1;
            C.pop();
            continue;
        }
        cout << A.top().x+B.top().x+C.top().x;
        return 0;
    }
    cout << -1;
    return 0;
}