Skip to content

Commit

Permalink
dp
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Feb 10, 2019
1 parent 65e13c8 commit 43f67a0
Show file tree
Hide file tree
Showing 5 changed files with 457 additions and 1 deletion.
2 changes: 1 addition & 1 deletion myCpps/!-OJcreater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace std;

//***************************************
const string CPPfile = "testTheRods_UVA10086"; //*
const string CPPfile = "expressionAgain_UVA10690"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
Expand Down
62 changes: 62 additions & 0 deletions myCpps/expressionAgain_UVA10690.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <bits/stdc++.h>

using namespace std;

ifstream fin("expressionAgain_UVA10690.in");
ofstream fout("expressionAgain_UVA10690.out");

int main()
{
while (true)
{
int n = 0, m = 0; fin >> n >> m;
if (n + m == 0) break;

if (n > m) swap(n, m);

int s = n + m;
vector<int> a(s);

for (int i = 0; i <= s - 1; ++i)
{
fin >> a[i];
}
sort(a.begin(), a.end());

int total = 0;

vector<vector<bool>> dp(n + 1, vector<bool>(5001, false));
dp[0][2500 + 0] = 1;

for (int i = 1; i <= s; ++i)
{
total += a[i - 1];
for (int j = min(i, n); j >= 1; --j)
{
for (int k = -2500; k <= 2500; ++k)
{
if (dp[j - 1][2500 + k] > 0)
{
dp[j][2500 + k + a[i - 1]] = true;
}
}
}
}

int ansMax = INT_MIN, ansMin = INT_MAX;
for (int k = -2500; k <= 2500; ++k)
{
if (dp[n][k + 2500] == true)
{
int now = k * (total - k);

ansMax = max(ansMax, now);
ansMin = min(ansMin, now);
}
}

fout << ansMax << ' ' << ansMin << '\n';
}

return 0;
}
Loading

0 comments on commit 43f67a0

Please sign in to comment.