题面链接 problem.pdf
题解链接 solution.pdf
Problem 1 跳跃(jump)
此题暂无题目链接
此题分类讨论即可(讨论与的大小关系)
AC 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll n, x;
ll solve(ll n, ll x) {
if (x == n) return 0;
if (x < n) return n - x;
ll cnt = 0;
while (x > n) x /= 2, cnt++;
cnt += (n - x);
return cnt;
}
int main() {
freopen("jump.in", "r", stdin);
freopen("jump.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
cin >> n >> x;
cout << solve(n, x);
return 0;
} Problem 2 技能学习 (skill)
此题也是数学题
重点就是要弄懂平均分绝对比都分给一个人好,因为这样超出的数越小,也就是消耗掉的更少,故尽量均分即可
AC 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll n, m, k, q, t;
int main() {
freopen("skill.in", "r", stdin);
freopen("skill.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
cin >> n >> m >> k >> q >> t;
ll max_stu = m / k;
max_stu = min(max_stu, n);
if (m % max_stu == 0) {
ll div = m / max_stu;
if (div * t <= q) cout << k * t * max_stu;
else cout << q * max_stu;
}
else {
ll div = m / max_stu, lst = m % max_stu;
if ((div + 1) * t <= q) cout << (div * (max_stu - lst) + lst * (div + 1)) * t;
else if (div * t <= q) cout << div * (max_stu - lst) * t + lst * q;
else cout << q * max_stu;
}
return 0;
}Problem 3 最长不下降子序列(lis)
观察样例不难发现最长部分就为型,但考试时并未注意到该序列不需要连续,故WA 27分
WA 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 1e6 + 5;
int n, x[N], ans;
bool f1 = false, f2 = false;
int main() {
freopen("lis.in", "r", stdin);
freopen("lis.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &x[i]), f1 = f1 || x[i] == 1, f2 = f2 || x[i] == 2;
if (!f1 || !f2) {
cout << n;
return 0;
}
int sum = 0, lst_sum = 0;
for (int i = 1; i <= n; i++) {
if (x[i - 1] - x[i] == 1) ans = max(ans, sum + lst_sum), lst_sum = sum, sum = 0;
sum++;
}
ans = max(ans, sum + lst_sum);
cout << ans;
return 0;
}正解也是使用DP
AC 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int dp[4], n, x;
int main() {
freopen("lis.in", "r", stdin);
freopen("lis.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
int dp0 = dp[0], dp1 = dp[1], dp2 = dp[2], dp3 = dp[3];
dp[0] += (x == 1);
dp[1] = max(dp0, dp1) + (x == 2);
dp[2] = max(dp1, dp2) + (x == 1);
dp[3] = max(dp2, dp3) + (x == 2);
}
cout << max({dp[0], dp[1], dp[2], dp[3]});
return 0;
}其中dp[0],dp[1],dp[2],dp[3]分别是1,2,1,2型的四个部分
Problem 4
第四题在考场上并未想到是利用乘法原理,于是直接暴力,TLE-36分
TLE 部分分代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 2e5 + 5, mod = 998244353;
int t, n, k, a[N], b[N], ans;
void del(int x) {
for (int i = x; i < n; i++) a[i] = a[i + 1];
n--;
}
void ins(int x, int num) {
n++;
for (int i = n; i >= x + 1; i--) a[i] = a[i - 1];
a[x] = num;
}
void dfs(int cur) {
if (cur == k + 1) {
ans++;
ans %= 998244353;
return ;
}
for (int i = 1; i <= n; i++) {
if (a[i] == b[cur]) {
//i - 1
if (i >= 2) {
int temp = a[i - 1];
del(i - 1);
dfs(cur + 1);
ins(i - 1, temp);
}
if (i < n) {
int temp = a[i + 1];
del(i + 1);
dfs(cur + 1);
ins(i + 1, temp);
}
break;
}
}
}
int main() {
freopen("operation.in", "r", stdin);
freopen("operation.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> k;
ans = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= k; i++) cin >> b[i];
dfs(1);
cout << ans << endl;
}
return 0;
}正解就是前面说的乘法原理,将 序列的每一位的选择情况数相乘即可
Tips:千万别忘了要开long long,以及要取模,多测还要清空!!!
AC 代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 2e5 + 5, mod = 998244353;
ll t, n, k, b[N], pos[N];
bool invalid[N];
int main() {
freopen("operation.in", "r", stdin);
freopen("operation.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> k;
ll ans = 1, temp;
memset(invalid, 0, sizeof(invalid));
memset(pos, 0, sizeof(pos));
invalid[0] = invalid[n + 1] = 1;
for (int i = 1; i <= n; i++) cin >> temp, pos[temp] = i;
for (int i = 1; i <= k; i++) cin >> b[i], invalid[pos[b[i]]] = true;
for (int i = 1; i <= k; i++) {
int cur_choice = 2 - invalid[pos[b[i]] - 1] - invalid[pos[b[i]] + 1];
ans = ans * cur_choice % mod;
if (!ans) break;
invalid[pos[b[i]]] = false;
}
cout << ans << endl;
}
return 0;
}本次考试本人总分 分,排名第二