5.3 比赛总结

· · 算法·理论

主编:yqx34

制作人员:yqx34、yyq3、Starlight_Footprint

5.3 比赛总结

0 赛后感

好难啊!
真的是太难了……

不过还好,总共 10 人,我拿第 5 名。
但只有 82 分……
看到结果的那一刻我震惊了!

1 扑克 Poker

评价:普通·橙

考点:排序(Sorting) & 去重(Unique)

考试题目链接

标准题目链接

一道很考验 STL 的题目。

首先,依照题目,把数据全部读入(建议这里用 vector )。然后用 STL 自带的 unique 去重:

cin >> n >> k;
vector <int> a(n);
for (int i = 0;i < n;i++) cin >> a[i];
sort(a.begin(),a.end());
auto new_end = unique(a.begin(),a.end());
a.erase(new_end,a.end());

接下来,定义变量 rlen 存储最长顺子长度,枚举 vector ,分类讨论:

2. $a_i - 1 \neq a_{i - 1}$(即非顺子,断连) 判断 $rlen$ 是否大于 $k$ ;若大于等于,已经有解,直接输出 $Yes$ 并结束程序;若小于,将 $rlen$ 归 $1$ ,重新计数。 ```cpp for (int i = 1;i < (int)a.size();i++) { if (a[i] - 1 == a[i - 1]) rlen++; else if (rlen >= k) { cout << "Yes\n"; return 0; } else rlen = 1; } ``` 最后,还要再判断 $rlen$ 是否大于 $k$ ,因为最后还有一段未检测的顺子,还要再检测一遍。 ```cpp if (rlen >= k) cout << "Yes\n"; else cout << "No\n"; ``` 综合代码: ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,rlen = 1; cin >> n >> k; vector <int> a(n); for (int i = 0;i < n;i++) cin >> a[i]; sort(a.begin(),a.end()); auto new_end = unique(a.begin(),a.end()); a.erase(new_end,a.end()); for (int i = 1;i < (int)a.size();i++) { if (a[i] - 1 == a[i - 1]) rlen++; else if (rlen >= k) { cout << "Yes\n"; return 0; } else rlen = 1; } if (rlen >= k) cout << "Yes\n"; else cout << "No\n"; return 0; } ``` ## 2 相聚 いっしょ 评价:顶级·黄 考点:贪心(Greedy Algorithm) & 动态规划(DP) [考试题目链接](https://www.luogu.com.cn/problem/T758085?contestId=324948) [标准题目链接](https://www.luogu.com.cn/problem/P11686) 一道纯种数学题! 首先考虑贪心:根据结论,最优情况应将 $2$ 只或 $3$ 只河狸聚在一起;如果有 $4$ 只及以上的河狸聚在一起,那肯定不是最优解。 贪心结束了,该到 $DP$ 了。 设 $dp_i$ 为前 $i$ 只河狸所花费的最小价钱,则有如下: - $dp_0 = 0$ (无成本) - $dp_1 = \infty$ (无解) - $dp_2 = a_2 - a_1

综合代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3,INF = 1e9;
int n,a[N],dp[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    sort(a + 1,a + n + 1);
    dp[1] = INF,dp[2] = a[2] - a[1];
    for (int i = 3;i <= n;i++) dp[i] = min(dp[i - 2] + a[i] - a[i - 1],dp[i - 3] + a[i] - a[i - 2]);
    cout << dp[n] << "\n";
    return 0;
}

3 交换门票 Increase Chocolates

评价:夯爆了·黄

考点:背包DP(‌Knapsack DP)

考试题目链接

标准题目链接

背包DP好题!让我爆了零……

题面很简洁,没有任何要提醒的;考场上想到了DP,但没想到是背包DP……

用 dp_{i,j} 表示使用前 i 种操作方案,初始有 j 张门票时,最多额外获得 dp_{i,j} 块巧克力,则有转移方程:

dp_{i,j} = \begin{cases} dp_{i-1,j} &j < a_i\\ max(dp_{i-1,j},dp_{i,j - a_i + b_i}) &j \geq a_i \end{cases}

根据转移方程,就能打出代码。

综合代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3,M = 503;
struct Swap { int a,b; }t[N];
int n,m,dp[M][N];

bool cmp(Swap x,Swap y) { return x.a < y.a; }

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> m >> n;
    for (int i = 1;i <= m;i++) cin >> t[i].a >> t[i].b;
    sort(t + 1,t + m + 1,cmp);
    for (int i = 1;i <= m;i++)
    {
        for (int j = 1;j < t[i].a;j++) dp[i][j] = dp[i - 1][j];
        for (int j = t[i].a;j <= n;j++) dp[i][j] = max(dp[i - 1][j],dp[i][j - t[i].a + t[i].b] + t[i].b);
    }
    for (int i = 1,l = 1;i <= n;l = dp[m][i] + i + 1,i++)
    {
        for (int j = l;j <= min(dp[m][i] + i,n);j++) cout << i << "\n";
        if (dp[m][i] + i >= n) break;
    }
    return 0;
}

4 雪球2 Snowball2

评价:顶级·黄

考点:分治(Partition) & 前缀和(Prefix Sum)

考试题目链接

标准题目链接

一道思路最难想,而且难写的题。

QWQ$~~因为不开long long被扣了 85 分~~$QWQ

对于一个雪球数列,多划分几次,若所有雪球都能被分成单个单个的,那就可以成立。

因此,我们要有一个函数,让数列劈成两份(数量可以不等),两份之差不超过 1 ,并再单独对两个数列做划分操作。

综合代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 3;
int n;
long long a,s[N];

bool ice(int l,int r)
{
    if (l == r) return true;
    for (int i = l;i < r;i++) if (s[i] - s[l - 1] - s[r] + s[i] >= 0 && s[i] - s[l - 1] - s[r] + s[i] <= 1 && ice(l,i) && ice(i + 1,r)) return true;
    return false;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1;i <= n;s[i] = s[i - 1] + a,i++) cin >> a;
    if (ice(1,n)) cout << "Yes\n";
    else cout << "No\n";
    return 0;
}

完结