国庆-S组-第一场

· · 个人记录

概况

传送门

题目编号 T1 T2 T3 T4
题目难度 黄 绿 绿 蓝
考察知识点 邻项交换类型贪心 果子合并类型堆贪心 排序不等式优化 果子合并类型堆贪心
拿分 100 60 0 0

今天上午将的是邻项交换类型贪心,总结直接融入 T1 分析了

分析

T1

类型: 邻项交换类型贪心

题意: 人都能看懂

思路:

邻项交换 x, y。对于后面的答案没有影响,但对于这两项:

  • 当 x 压在 y 上面:
    $y$ 的**压扁指数** $ = sum + x_w - y_s$ ( $sum + x_w$ 才是 $y$ **上**面压着的所有奶牛重量和) 所以 $x$ 和 $y$ 两头奶牛之间的**总压扁指数**就是 $max(sum - x_s,sum + x_w - y_s)$ ,因为都有一个 $sum$ ,所以可以变成: $sum + max(- x_s,sum + x_w - y_s)
  • 当 x 被压在 y 下面:
    $y$ 的**压扁指数** $ = sum - y_s$ ( $sum$ 是 $y$ **上**面压着的所有奶牛重量和) 所以 $x$ 和 $y$ 两头奶牛之间的**总压扁指数**就是 $max(sum + y_w - x_s,sum - y_s)$ ,因为都有一个 $sum$ ,所以可以变成: $sum + max(y_w - x_s,-y_s)

那么题目要求的是总压扁指数最小,那么就要看两种情况的总压扁指数较小就采用情况。所以就要比较上面两个式子,CMP应该这么写:

bool cmp(node x, node y)
{
    int ans = max(-x.s, x.w - y.s);
    int cnt = max(y.w - x.s, -y.s); 
    return ans < cnt; // “<” 还是 “>” 此处不关键
}

上面给出的CMP写法排序后是下标越小的压在越上面,所以排序后应该这么计算答案:

int cnt = 0, mmax = -1e18;
for (int i = 1; i <= n; i++)
{
  mmax = max(mmax, cnt - a[i].s);
  cnt += a[i].w; // cnt 表示 i 上面的所有奶牛重量和
}
cout << mmax;

ACcode

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e4 + 10;
int n;
struct node
{
    int w, s;
};
node a[N];
bool cmp(node x, node y)
{
    int ans = max(-x.s, x.w - y.s);
    int cnt = max(y.w - x.s, -y.s); 
    return ans < cnt;
}
signed main()
{
//  freopen("T1.in", "r", stdin);
//  freopen("T1.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i].w >> a[i].s;
    sort(a + 1, a + n + 1, cmp);
    int cnt = 0, mmax = -1e18; // 注意答案有可能是负数
    for (int i = 1; i <= n; i++)
    {
        mmax = max(mmax, cnt - a[i].s);
        cnt += a[i].w;
    }
    cout << mmax;
    return 0;
}

T2

类型: 果子合并类型堆贪心

题意: 人都能看懂

思路:

就只是把合并果子加强一下数据而已。60分的古董:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e3 + 10;
struct node
{
int x;
bool operator < (const node & tmp) const {
return x > tmp.x;
}
};
priority_queue<node> q;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
int a;
cin >> a;
q.push({a});
}
int ans = 0;
while (q.size() > 1)
{
int a = q.top().x;
q.pop();
int b = q.top().x;
q.pop();
ans += a + b;
q.push({a + b}); 
}
cout << ans;
return 0;
}

优化一下,想到是 push 和 pop 的速度太慢。想到用两个队列,一个队列存还没有合并的,一个队列存合并过的。反正答案一定来自于两个队列的队首,而且新组成的两个堆一定比原来的堆大,加入到存储已经合并的队列就行。但是首先你要保证的就是还没有合并过的队列一开始必须是有序的,不然还得用优先队列,由此我们先要用桶排搞定还没有合并的队列。

ACcode

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n;
void read(int &x)
{
    int f = 1;
    x = 0;
    char s = getchar();
    while (s < '0' || s > '9')
    {
        if (s == '-') f = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9')
    {
        x = x * 10 + s - '0';
        s = getchar();
    }
    x *= f;
}
int pail[N];
queue<int> q1, q2;
signed main()
{
//  freopen("T2.in", "r", stdin);
//  freopen("T2.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int x;
        read(x);
        pail[x]++;
    }
    for (int i = 1; i <= 100000; i++)
    {
        while (pail[i])
        {
            pail[i]--;
            q1.push(i);
        }
    }
    int ans = 0;
    for (int i = 1; i <= n - 1; i++)
    {
        int x, y;
        if ((!q1.empty() && q1.front() < q2.front()) || q2.empty())
        {
            x = q1.front();
            q1.pop();
        }
        else
        {
            x = q2.front();
            q2.pop();
        }
        if ((!q1.empty() && q1.front() < q2.front()) || q2.empty())
        {
            y = q1.front();
            q1.pop();
        }
        else
        {
            y = q2.front();
            q2.pop();
        }
        ans += x + y;
        q2.push(x + y);
    }
    cout << ans;
    return 0;
}

T3

套路: 排序不等式优化

题意: 确定 a 和 b 两个序列的顺序,使 ∑(a_i - b_i) ^2 最小。

思路:

就是使 a_i - b_i 变小,所以 a 和 b 都升序排序后有最小值。但不一定要全部排序,只需要元素对应即可。就是 a 最大的和 b 最大的、a 次大的和 b 次大的 ...... a 最小的和 b 最小的。

所以就是要我们求在只允许相邻交换使如何满足条件,就变成了逆序对:

ACcode

线段树

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 7;
const int mod = 1e8 - 3;
int n, c[maxn], t[maxn], ans;
struct Node
{
    int id, v;
} a[maxn], b[maxn];
bool cmp(Node a, Node b)
{
    return a.v < b.v;
}
int lowbit(int x)
{
    return x & -x;
}
void modify(int x, int v)
{
    while (x <= n)
    {
        t[x] += v;
        x += lowbit(x);
    }
}
int query(int x)
{
    int sum = 0;
    while (x > 0)
    {
        sum += t[x];
        x -= lowbit(x);
    }
    return sum;
}
signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].v, a[i].id = i;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i].v, b[i].id = i;
    }
    sort(a + 1, a + 1 + n, cmp);
    sort(b + 1, b + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
    {
        c[b[i].id] = a[i].id;
    }
    for (int i = n; i; i--)
    {
        ans += query(c[i]);
        modify(c[i], 1);
    }
    cout << ans % mod << "\n";
    return 0;
}