# 2023.2.16 普及组

Yapink 2023-02-16 21:20:44

``````#include <iostream>

using namespace std;

const int N = 100010;

int main()
{
int n;
scanf("%d", &n);

int res = 0;
for (int i = 1; i <= n; i ++ ) res += n / i;
printf("%d\n", res);

return 0;
}``````

第二题：贪心模板题

``````#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 50010;

int n, w, s;
PII cow[N];

int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
{
int w, s;
cin >> w >> s;
cow[i] = {w + s, w};
}

sort(cow, cow + n);

int res = -2e9, sum = 0;
for (int i = 0; i < n; i ++ )
{
int w = cow[i].second, s = cow[i].first - cow[i].second;
res = max(res, sum - s);
sum += w;
}

cout << res << endl;
return 0;
}``````

第三题：01背包多一重限制，只是多一重循环，多一维数组

``````#include <iostream>

using namespace std;

const int N = 55, T = 410;

int n, V, M;
int f[T][T];

int main()
{
scanf("%d%d%d", &V, &M, &n);

for (int i = 1; i <= n; i ++ )
{
int v, m, k;
cin >> v >> m >> k;
for (int j = V; j >= v; j -- )
for (int u = M; u >= m; u -- )
f[j][u] = max(f[j][u], f[j - v][u - m] + k);
}

cout << f[V][M] << endl;
return 0;
}``````
...全文
13 回复 打赏 收藏 举报

3.5w+

2023-02-16 21:20

刷题！