AC,但是关于背包的疑问
  • 板块CF864E Fire
  • 楼主Vitamin_B
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/15 21:51
  • 上次更新2024/3/16 08:53:43
查看原帖
AC,但是关于背包的疑问
743373
Vitamin_B楼主2024/3/15 21:51

rt,这题我用的不是滚动数组,因此按理说第二维枚举时间时正序和逆序枚举没关系,但逆序AC,正序WA#14(第一问少1,第二问AC),这是为什么?

AC:

# include <bits/stdc++.h>

# define ffor(i,name) \
	for (auto i = name.begin (); i != name.end (); ++ i)

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

struct node {

	int t, d, p, id;

	bool operator < (const node& x) const {

		return d < x.d;

	}

} a[105];

int n, dp[105][2005], m, maxi, lst[105][2005];

vector <int> ans;

inline void dfs (int step, int x) {
//	cout << x << ' ' << lsti[x] << ' ' << lstx[x] << '\n';
	if (! step)
		return ;

	dfs (step - 1, lst[step][x]);

	if (lst[step][x] < x)
		ans.emplace_back (a[step].id);

	return ;

}

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n;

	for (int i = 1; i <= n; ++ i) {

		cin >> a[i].t >> a[i].d >> a[i].p;

		a[i].id = i;

		m = max (m, a[i].d);

	}

	sort (a + 1, a + n + 1);

	for (int i = 1; i <= n; ++ i) {

		for (int j = 1; j < m; ++ j)
			dp[i][j] = dp[i - 1][j], lst[i][j] = j;

		for (int j = a[i].d - 1; j >= a[i].t; -- j) //逆序
			if (dp[i][j - a[i].t] + a[i].p > dp[i][j])
				dp[i][j] = dp[i - 1][lst[i][j] = j - a[i].t] + a[i].p;

	}

	for (int i = 1; i < m; ++ i)
		if (dp[n][i] > dp[n][maxi])
			maxi = i;

	dfs (n, maxi);

	cout << dp[n][maxi] << '\n' << ans.size () << '\n';

	for (int i : ans)
		cout << i << ' ';

	return 0;

}
/*
20
15 185 14
19 197 20
3 109 13
1 206 20
1 191 7
7 202 6
17 107 5
2 105 11
13 178 9
2 209 6
15 207 15
12 200 5
16 60 13
19 125 19
12 103 3
4 88 13
15 166 3
18 154 12
5 122 2
15 116 4
*/

WA:

# include <bits/stdc++.h>

# define ffor(i,name) \
	for (auto i = name.begin (); i != name.end (); ++ i)

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

struct node {

	int t, d, p, id;

	bool operator < (const node& x) const {

		return d < x.d;

	}

} a[105];

int n, dp[105][2005], m, maxi, lst[105][2005];

vector <int> ans;

inline void dfs (int step, int x) {
//	cout << x << ' ' << lsti[x] << ' ' << lstx[x] << '\n';
	if (! step)
		return ;

	dfs (step - 1, lst[step][x]);

	if (lst[step][x] < x)
		ans.emplace_back (a[step].id);

	return ;

}

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n;

	for (int i = 1; i <= n; ++ i) {

		cin >> a[i].t >> a[i].d >> a[i].p;

		a[i].id = i;

		m = max (m, a[i].d);

	}

	sort (a + 1, a + n + 1);

	for (int i = 1; i <= n; ++ i) {

		for (int j = 1; j < m; ++ j)
			dp[i][j] = dp[i - 1][j], lst[i][j] = j;

		for (int j = a[i].t; j < a[i].d; ++ j) //正序
			if (dp[i][j - a[i].t] + a[i].p > dp[i][j])
				dp[i][j] = dp[i - 1][lst[i][j] = j - a[i].t] + a[i].p;

	}

	for (int i = 1; i < m; ++ i)
		if (dp[n][i] > dp[n][maxi])
			maxi = i;

	dfs (n, maxi);

	cout << dp[n][maxi] << '\n' << ans.size () << '\n';

	for (int i : ans)
		cout << i << ' ';

	return 0;

}
/*
20
15 185 14
19 197 20
3 109 13
1 206 20
1 191 7
7 202 6
17 107 5
2 105 11
13 178 9
2 209 6
15 207 15
12 200 5
16 60 13
19 125 19
12 103 3
4 88 13
15 166 3
18 154 12
5 122 2
15 116 4
*/
2024/3/15 21:51
加载中...