小乌龟的题。
每个乌龟有自身重量又有可承载的重量
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXn = 6000;
const int MAX = 0x7ffffff;
struct Turtle
{
int wei,stg;
}t[MAXn];
int w,s,n,dp[MAXn];
bool cmp(const Turtle &a,const Turtle &b)
{
if(a.stg != b.stg)
{
return a.stg < b.stg;
}
return a.wei < b.wei;
}
int main()
{
while(~scanf("%d %d",&w,&s))
{
if(w <= s)
{
t[++n].wei = w;
t[n].stg = s;
}
}
sort(t + 1, t + n + 1, cmp);
for (int i = 1; i <= n; i++)
dp[i] = MAX;
int ans = (n ? 1 : 0);
for (int i = 1; i <= n; i++)
for (int j = n; j >= 1; j--) {
if (t[i].stg >= dp[j - 1] + t[i].wei)
dp[j] = min(dp[j], dp[j - 1] + t[i].wei);
if (dp[j] < MAX)
ans = max(ans, j);
}
printf("%d\n", ans);
return 0;
}
本文探讨了一种算法,用于解决小乌龟的自身重量和可承载重量之间的分配问题。通过使用优先级队列和动态规划方法,实现最优解决方案。

906

被折叠的 条评论
为什么被折叠?



