暑假-S组-第八场

· · 个人记录

概述

传送门

回到中位数

题目编号 T1 T2 T3 T4
题目难度 黄 黄 黄 绿
考察知识 桶 + 双指针 数学 + 贪心 模拟 + 推导 数学 + 二分

分析

T1

Wrong Cause

发现了需要使用双指针,但不知道既然要桶

Solution

Num.1 \color{red}15pts

考虑暴力搜索,两个一组枚举,O(n^3)

Num.2 \color{orange}50pts

发现 w 的数据范围较小,我们枚举 w 的总和,分组自然是大的和小的搭配(因为得到的组数一定最多),所以排序之后就双指针即可

Num.3 \color{green}100pts

双指针算法还是慢了。由之前发现了 w 的范围小,所以我们可以拆分 w ,比如 总和 = 100 的时候,枚举一个 w1 ,得到另外一个 w2 = 100 - w1 ,直接用桶存储来二者的数量,取最小值,但是需要特殊注意二者相等的情况。此时的时间复杂度是:O(k∗T) ,k 的范围是 [0,20000],炸不了一点

ACcode

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int a[N], n, pail[N];
void solve()
{
    int mmax = 0;
    for (int w = 2; w <= 200; w++)
    {
        int cnt = 0;
        for (int i = 1; i <= 100; i++)
        {
            if (w - i >= 1)
            {
                int x = i, y = w - i;
                if (x > y) continue;
                if (x == y) cnt += pail[x] / 2;
                else cnt += min(pail[x], pail[y]);
            }
        }
        mmax = max(mmax, cnt);
    }
    cout << mmax << "\n";
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i<= n; i++)
        {
            cin >> a[i];
            pail[a[i]] ++;
        }
        solve();
        for (int i = 1; i <= 100; i++) pail[i] = 0;
    }
    return 0;
}

T2

Wrong Cause

思路算是找对了,但是忽略掉了递归出答案后的还是在递归浪费时间。

Solution

  • 想让最后的结果小,那么就有了下面两条有优先级的原则:
  1. 尽量位数小
  2. 尽量小数码在前
  • 于是从 9 开始往 2 考虑,只要是因数就除。除到出不了为止

ACcode

#include <bits/stdc++.h>
#define int long long
using namespace std;
int solve(int x)
{
    if (x < 10) return x + 10;
    vector<int> ans;
    while (x > 9)
    {
        bool flag = 0;
        for (int i = 9; i >= 2; i--)
        {
            if (x % i == 0)
            {
                flag = 1;
                ans.push_back(i);
                x /= i;
                break;
            }
        }
        if (!flag) return -1;
    }
    ans.push_back(x);
    sort(ans.begin(), ans.end());
    int sum = 0;
    for (int i = 0; i < ans.size(); i++)
        sum = sum * 10 + ans[i];
    return sum;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        cout << solve(n) << "\n";
    }
    return 0;
}

T3

Wrong Cause

写了个假解,运气好拿了 \color{green}90pts

Solution

  • 暴力模拟必然超时,观察发现每操作 4 次,得到的相对位移是固定的。那么先暴力求出一回 4 次,将相对位移重复计算(即乘)\frac{n}{4} 次,可能最后还剩了 n % 4 次操作,暴力即可

ACcode

#include <bits/stdc++.h>
using namespace std;
#define int long long  
const int N = 5e5 + 10;
int a[N];
int dx[10] = {-1, 0, 1, 0};
int dy[10] = {0, 1, 0, -1};
int n, t, p, x, y;
int sum = 0;
signed main()
{
    cin >> n >> t;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        x += dx[p] * a[i];
        y += dy[p] * a[i]; 
        p = (p + a[i]) % 4;
    }
    p = 0;
    int nx = 0, ny = 0;
    for (int i = 1; i <= t; i++)
    {
        p = (p + (sum % 4)) % 4;
        if (p == 0)
        {
            nx += x;
            ny += y; 
        }
        if (p == 1)
        {
            nx += y; 
            ny -= x; 
        }
        if (p == 2)
        {
            nx -= x; 
            ny -= y; 
        }
        if (p == 3)
        {
            nx -= y; 
            ny += x; 
        }
    } 
    cout << abs(nx) + abs(ny);
    return 0;
}