# 普及组题目打卡2.6日

2023-02-06 16:41:19

1.

01背包，选或者不选

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
#define x first
#define y second
#define ms(a,s) memset(a, s, sizeof a);
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
const int mod = 1000007;
const int inf = 0x3f3f3f3f;
const int N = 1010;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int w[N], v[N];
int f[N];
void slove() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i ++ ) {
cin >> w[i] >> v[i];
}
for(int i = 1; i <= m; i ++ ) {
for(int j = n; j >= w[i]; j -- ) {
f[j] = max(f[j], f[j - w[i]] + v[i]);
}
}
cout << f[n];
}
int main()
{
int t = 1;

//	cin >> t;

while(t --) {
slove();
}

return 0;
}``````

2.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
#define x first
#define y second
#define ms(a,s) memset(a, s, sizeof a);
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
const int mod = 1000007;
const int inf = 0x3f3f3f3f;
const int N = 1e7+10;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
ll w[N], v[N];
ll f[N];
void slove() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i ++ ) {
cin >> w[i] >> v[i];
}
for(int i = 1; i <= m; i ++ ) {
for(int j = w[i]; j <= n; j ++ ) {
f[j] = max(f[j], f[j - w[i]] + v[i]);
}
}
cout << f[n];
}
int main()
{
int t = 1;

//	cin >> t;

while(t --) {
slove();
}

return 0;
}``````

3.

``````#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int a[N][N];
int f[N][N];
void slove() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) {
for(int j = 1; j <= m; j ++ ) {
cin >> a[i][j];
}
}
for(int i = 1; i <= n; i ++ ) {
for(int j = 1; j <= m; j ++ ) {
f[i][j] = max(f[i][j - 1], f[i - 1][j]) + a[i][j];
}
}
cout << f[n][m] << '\n';
}

int main()
{
int t;
cin >> t;
while(t --) {
slove();
}
return 0;
}``````

4.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
#define x first
#define y second
#define ms(a,s) memset(a, s, sizeof a);
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
const int mod = 1e6+7;
const int inf = 0x3f3f3f3f;
const int N = 1010;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int a[N];
int f[N][N];
void slove() {
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> a[i];
f[0][0] = 1;
for(int i = 1; i <= n; i ++ ) {
for(int j = 0; j <= m; j ++ ) {
for(int k = 0; k <= min(j, a[i]); k ++ ) {
f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
}
}
}
cout << f[n][m];
}
int main()
{
int t = 1;

//	cin >> t;

while(t --) {
slove();
}

return 0;
}``````

...全文
16 回复 打赏 收藏 举报

3.5w+

2023-02-06 16:41

刷题！