5.3 比赛总结
主编:yqx34
制作人员:yqx34、yyq3、Starlight_Footprint
5.3 比赛总结
0 赛后感
好难啊!
真的是太难了……
不过还好,总共
但只有 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());
接下来,定义变量
-
dp_i = min(dp_{i - 2} + (a_i - a_{i - 1}),dp_{i - 3} + (a_i - a_{i - 2})) (i \geq 3)
综合代码:
#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……
用
根据转移方程,就能打出代码。
综合代码:
#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)
考试题目链接
标准题目链接
一道思路最难想,而且难写的题。
对于一个雪球数列,多划分几次,若所有雪球都能被分成单个单个的,那就可以成立。
因此,我们要有一个函数,让数列劈成两份(数量可以不等),两份之差不超过
综合代码:
#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;
}