第34次CCF计算机软件能力认证
(第一题)矩阵重塑(其一)
#include<iostream>
#include<algorithm>
using namespace std;
int n, m, p, q;
int main()
{
cin >> n >> m >> p >> q;
int k = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
{
int x;
cin >> x;
printf("%d ", x);
if(++k == q){
k = 0;
printf("\n");
}
}
}
(第二题)矩阵重塑(其二)
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e4 + 10;
int n, m, t;
int w[N];
int temp[N];
int main()
{
cin >> n >> m >> t;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> w[i * m + j];
while(t--)
{
int op, a, b;
cin >> op >> a >> b;
if(op == 1)
{
n = a, m = b;
}
else if(op == 2)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
temp[j * n + i] = w[i * m + j];
memcpy(w, temp, sizeof temp);
swap(n, m);
}
else if(op == 3)
{
cout << w[a * m + b] << endl;
}
}
}
(第四题)货物调度
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10, M = 4e4 + 10;
int n, m, v;
vector<int> w[N];//w[i][j]:第i个仓库中,第j大的货物价值
int b[N], c[N];//仓库的基本费用与计件费用
ll dp[N][M];//dp[i][j]:在前i个仓库中取货物,在总费用为j的情况下,获得的最大总价值,M为计算的最大总花费
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
cin >> n >> m >> v;
// for(int i=0;i<M;i++)dp[0][i]=0;
for(int i = 1; i <= n; i++) cin >> b[i] >> c[i];
for(int i = 1; i <= m; i++)
{
int a, b;
cin >> a >> b;
b++;
w[b].push_back(a);
}
for(int i = 1; i <= n; i++) sort(w[i].begin(), w[i].end(), cmp);//按货物价值降序排序,通过贪心思路进行DP获得最优解
int res = 0x3f3f3f3f;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < M; j++)
{
dp[i][j] = dp[i - 1][j];
ll sum = 0;
for(int k = 1; k <= w[i].size(); k++)
{
if(j < b[i] + k * c[i]) break;
sum += w[i][k - 1];
dp[i][j] = max(dp[i][j], dp[i - 1][j - (b[i] + k * c[i])] + sum - (b[i] + k * c[i]));
}
if(dp[i][j] >= v) res = min(res, j);
}
}
cout << res;
}