1000 a+b
题解:
? 普通整数加减
Code
#include<iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) {
cout << a+b << endl;
}
return 0;
}
1001 又是苹果(状态压缩)
题面:
? 传送门:https://acm.xidian.edu.cn/problem.php?id=1001
题解:
? 首先需要存储n·m个数据,(1<=n·m<=106)不可以直接开array[1e6][1e6],会爆掉,可以采用vector开二维数组
vector<vector<char>> matrix;
matrix = vector<vector<char>>(n+1, vector<char>(m+1));//申请内存
? 交换行列可以使用新建数组Line, Row,对数组的行列进行交换,达到映射交换的效果
Code:
#include<bits/stdc++.h>
#include<vector>
using namespace std;
vector<vector<char>> matrix;
int main() {
int n, m, t = 1;
while (cin >> n >> m) {
printf("Case #%d:\n", t++);
getchar();
matrix = vector<vector<char>>(n+1, vector<char>(m+1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
matrix[i][j] = getchar();
}
getchar();
}
vector<int> line(n+1), row(m+1);
for (int i = 1; i <= n; i++) line[i] = i;
for (int i = 1; i <= m; i++) row[i] = i;
int q; cin >> q;
while (q--) {
int op, a, b;
cin >> op >> a >> b;
if (op == 1) {
char ch = matrix[line[a]][row[b]];
if (ch == 'T')
puts("Tree");
else if (ch == 'i')
puts("Phone");
else
puts("Empty");
}
else if (op == 2) {
swap(line[a], line[b]);
}
else if(op == 3){
swap(row[a], row[b]);
}
}
}
return 0;
}
1002 小W的塔防(动态规划)
题面:
? 传送门:https://acm.xidian.edu.cn/problem.php?id=1002
题解:
? 怪兽在塔下所受到的伤害仅与其所经过的塔和当前塔的种类、个数有关,所以可以使用一个三维数组dp(i,j,k)表示通过了i个绿塔,j个蓝塔,k个红塔所受到的最大伤害。
? 所以dp(i,j,k) =max( dp(i-1,j,k)+(i-1)y(t+jz), max (dp(i,j-1,k)+iy(t+(j-1)z), dp(i,j,k-1)+(iy+x)(t+jz)) )
code:
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 105;
LL dp[N][N][N];//表示有i个绿塔,有j个蓝塔,k个红塔
int main() {
LL n, x, y, z, t;
while (scanf("%lld %lld %lld %lld %lld", &n, &x, &y, &z, &t) != EOF) {
memset(dp, 0, sizeof(dp));
LL ans = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n-i; j++) {
for (int k = 0; k <= n-i-j; k++) { //怪通过i个绿塔,j个蓝塔,k个红塔
LL t1 = i? dp[i-1][j][k]+(i-1)*y*(t+j*z):0;
LL t2 = j? dp[i][j-1][k]+i*y*(t+(j-1)*z):0;
LL t3 = k? dp[i][j][k-1]+(i*y+x)*(t+j*z):0;
dp[i][j][k] = max(t1, max(t2, t3));
}
ans = max(ans, dp[i][j][n-i-j]);
}
}
printf("%lld\n", ans);
}
return 0;
}
|