Skip to content

Commit

Permalink
AcWing 3445. 点菜问题
Browse files Browse the repository at this point in the history
Signed-off-by: Tategoto Azarasi <2724167997@qq.com>
  • Loading branch information
tategotoazarasi committed Aug 22, 2024
1 parent 828d80a commit 40820ed
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
35 changes: 35 additions & 0 deletions acwing408.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1830,4 +1830,39 @@ namespace acwing {
return 0;
}
}// namespace acwing2

/**
* @brief 3445. 点菜问题
*/
namespace acwing3445 {
int main(istream &cin, ostream &cout) {
int c, n;
cin >> c >> n;
vector<int> p = vector<int>(n);
vector<int> v = vector<int>(n);
for(int i = 0; i < n; i++) {
cin >> p[i] >> v[i];
}
vector<status> dp = vector<status>(c + 1, status{
.v = 0,
.free = vector<bool>(n, true)});
int max_v = 0;
for(int i = 0; i <= c; i++) {
for(int j = 0; j < n; j++) {
max_v = max(max_v, dp[i].v);
if(dp[i].free[j]) {
int next_v = dp[i].v + v[j];
int next_c = i + p[j];
if(next_c <= c && next_v > dp[next_c].v) {
dp[next_c].v = next_v;
dp[next_c].free = vector<bool>(dp[i].free);
dp[next_c].free[j] = false;
}
}
}
}
cout << max(max_v, dp[c].v);
return 0;
}
}// namespace acwing3445
}// namespace acwing
11 changes: 11 additions & 0 deletions acwing408.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,17 @@ namespace acwing {
};
int main(istream & /*cin*/, ostream & /*cout*/);
}// namespace acwing2

/**
* @brief 3445. 点菜问题
*/
namespace acwing3445 {
struct status {
int v;
vector<bool> free;
};
int main(istream & /*cin*/, ostream & /*cout*/);
}// namespace acwing3445
}// namespace acwing

#endif//PROBLEMSCPP_ACWING408_H
45 changes: 45 additions & 0 deletions acwing408_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1999,4 +1999,49 @@ namespace acwing {
ASSERT_EQ("8", ans);
}
}// namespace acwing2

/**
* @brief 3445. 点菜问题
*/
namespace acwing3445 {
TEST(acwing3445, case1) {
istringstream in("90 4\n"
"20 25\n"
"30 20\n"
"40 50\n"
"10 18");
auto out = ostringstream();
main(in, out);
const auto ans = out.str();
ASSERT_EQ("95", ans);
}

TEST(acwing3445, case2) {
istringstream in("300 20\n"
"19 24\n"
"26 3\n"
"2 5\n"
"17 8\n"
"13 17\n"
"10 9\n"
"19 9\n"
"30 4\n"
"23 24\n"
"9 9\n"
"5 22\n"
"10 15\n"
"9 10\n"
"15 29\n"
"20 14\n"
"8 12\n"
"4 23\n"
"24 22\n"
"16 13\n"
"5 26");
auto out = ostringstream();
main(in, out);
const auto ans = out.str();
ASSERT_EQ("298", ans);
}
}// namespace acwing3445
}// namespace acwing

0 comments on commit 40820ed

Please sign in to comment.