HDU-1160-FatMouse's Speed(DP, 最长递增子序列)
链接:
https://vjudge.net/problem/HDU-1160
题意:
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
思路:
对s排序, 然后最长递增子序列,nlogn总有bug还是n2好用
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 1e3+10;
struct Node
{
int w, s;
int id;
bool operator < (const Node& that) const
{
return this->s > that.s;
}
}node[MAXN];
int Dp[MAXN], Pre[MAXN];
int n;
void Print(int p)
{
if (p == -1)
return;
Print(Pre[p]);
printf("%d\n", node[p].id);
}
int main()
{
int l, r;
while (~scanf("%d%d", &l, &r))
{
node[++n].w = l;
node[n].s = r;
node[n].id = n;
}
sort(node+1, node+1+n);
memset(Pre, -1, sizeof(Pre));
memset(Dp, 0, sizeof(Dp));
for (int i = 1;i <= n;i++)
{
Dp[i] = 1;
for (int j = 1;j < i;j++)
{
if (node[i].w > node[j].w && node[i].s < node[j].s)
{
if (Dp[j]+1 > Dp[i])
{
Dp[i] = Dp[j]+1;
Pre[i] = j;
}
}
}
}
int maxval = 1, maxpos = 1;
for (int i = 1;i <= n;i++)
{
if (maxval < Dp[i])
{
maxval = Dp[i];
maxpos = i;
}
}
printf("%d\n", maxval);
Print(maxpos);
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· .NET 8.0 + Linux 香橙派,实现高效的 IoT 数据采集与控制解决方案
· DeepSeek处理自有业务的案例:让AI给你写一份小众编辑器(EverEdit)的语法着色文件