求最大的重叠区间个数
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Node
{
public:
int value;
int flag;
Node(int value, int flag) :value(value), flag(flag) {}
bool operator<(const Node&node)
{
if (this->value == node.value)
return this->flag > node.flag;
return this->value < node.value;
}
};
int main()
{
int N;
cin >> N;
vector<Node>myvec;
for (int i = 0; i < N; i++)
{
int begin, end;
cin >> begin;
myvec.push_back(Node(begin, 0));
cin >> end;
myvec.push_back(Node(end,1));
}
sort(myvec.begin(),myvec.end());
int overlap_num = 0;
int res = 0;
for (auto&elem:myvec)
{
if (elem.flag == 0)
{
overlap_num++;
res = max(overlap_num, res);
}
else
{
overlap_num--;
}
}
cout << res << endl;
system("pause");
return 0;
}
简单的完全背包问题
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int total_money;
cin >> total_money;
int n;
cin >> n;
vector<int>prices(n + 1, 0);
vector<int>values(n + 1, 0);
for (int i = 1; i <= n; i++)
{
cin >> prices[i];
cin >> values[i];
}
//dp[i][j]表示当预算为j时,前i件物资的最优组合对应的最大价值
vector<vector<int>>dp(n + 1, vector<int>(total_money + 1, 0));
for(int i=1;i<=n;i++)
for (int j = 1; j <= total_money; j++)
{
if (j < prices[i])
{
dp[i][j] = dp[i - 1][j];
}
else
{
dp[i][j] = max(dp[i][j - prices[i]] + values[i],max(dp[i - 1][j], dp[i-1][j - prices[i]] + values[i]));
}
}
cout << dp[n][total_money] << endl;
system("pause");
return 0;
}