考试情况
| 题目 | 分数 | 改题用时 | 改后分数 |
|---|---|---|---|
| A | 100 | 0s | 100 |
| B | 20 | 2s | 100 |
| C | 40 | 无 | 40 |
| D | 20 | 5min | 100 |
| E | 0 | 无 | 0 |
| F | 60 | 无 | 60 |
| G | 0 | 无 | 0 |
A
第一次就是满分
思路
用数组模拟每一次的操作
B
错误原因
思路
先从小到大排序,再求个前缀和以便后面求和时不用再次扫一遍,然后每次输入x, y就直接用前缀和数组求从n - (x - y)到n - x + 1的和即可
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 114;
int n, q, a[N], b[N], x, y;
int main(){
/// freopen("B.in", "r", stdin);
/// freopen("B.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++){
b[i] = b[i - 1] + a[i];
}
while(q--){
cin >> x >> y;
cout << b[n - (x - y)] - b[n - x] << '\n';
}
return 0;
}正确代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 114;
ll n, q, a[N], b[N], x, y;
int main(){
/// freopen("B.in", "r", stdin);
/// freopen("B.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++){
b[i] = b[i - 1] + a[i];
}
while(q--){
cin >> x >> y;
cout << b[n - (x - y)] - b[n - x] << '\n';
}
return 0;
}错误的地方
int n, q, a[N], b[N], x, y;
///变为 ll n, q, a[N], b[N], x, y;C
错误原因
骗分
错误代码
#include <bits/stdc++.h>
using namespace std;
int main(){
/// freopen("C.in", "r", stdin);
/// freopen("C.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << 0;
return 0;
}正确代码
还没改出来
D
错误原因
DP写了多余的初始化
思路
一维DP,DP[i]表示花i元能得到的最大价值
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 114, M = 1e5 + 114;
int t, n, P, Q, p[N], q[N], dp[M];
int main(){
/// freopen("D.in", "r", stdin);
/// freopen("D.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
cin >> n >> P >> Q;
fill(dp + 0, dp + Q * 2 + 5, 0);
for(int i = 1; i <= n; i++){
cin >> p[i] >> q[i];
dp[q[i]] = p[i];
}
for(int i = 1; i <= n; i++){
for(int j = q[i]; j <= Q; j++){
dp[j] = max(dp[j], dp[j - q[i]] + p[i]);
}
}
int ans = -1;
for(int i = 1; i <= Q; i++){
if(dp[i] >= P){
ans = i;
break;
}
}
cout << ans << '\n';
}
return 0;
}正确代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 114, M = 1e5 + 114;
ll t, n, P, Q, p[N], q[N], dp[M];
int main(){
/// freopen("D.in", "r", stdin);
/// freopen("D.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
cin >> n >> P >> Q;
fill(dp + 0, dp + Q * 2 + 5, 0);
for(int i = 1; i <= n; i++){
cin >> p[i] >> q[i];
}
for(int i = 1; i <= n; i++){
for(int j = Q; j >= q[i]; j--){
dp[j] = max(dp[j], dp[j - q[i]] + p[i]);
}
}
ll ans = -1;
for(int i = 0; i <= Q; i++){
if(dp[i] >= P){
ans = i;
break;
}
}
cout << ans << '\n';
}
return 0;
}错误的地方
dp[q[i]] = p[i];///不需要