J - FatMouse's Speed
p的思路不一定要到最后去找到ans;也可以设置成在中间找到ans;比如J - FatMouse's Speed 这个题,如果要是让dp[n]成为最终答案的话,即到了i,最差的情况也是dp[i-1],就很难去保存路径,但是如果换一个思路,让dp[i]必须去参与,如果无法与前面的结合,那么就新开一个。
最后路径是保存的逆序的,那么开一个stack就可以解决。
//显然这个题是要维护两个变量的最长上升子序列
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <stack> using namespace std;
//ifstream fin("a.txt");
struct node{
int weight,speed,num;
}a[];
bool cmp(node a,node b)
{
if(a.weight==b.weight)
return a.speed>b.speed;
else return a.weight<b.weight;
}
struct Node{
int cnt,now,pre;
}dp[];
int pre[];
int main()
{
int x,y;int i=;
while(cin>>x>>y)
{
a[i].weight=x;a[i].speed=y,a[i].num=i;i++;
}
sort(a+,a+i,cmp);
dp[].cnt=;
for(int j=;j<=i-;j++)
{
dp[j].cnt=;
for(int k=j-;k>=;k--)
{
if(a[j].speed<a[k].speed&&a[j].weight>a[k].weight)
{
if(dp[j].cnt<dp[k].cnt+)
{
dp[j].cnt=dp[k].cnt+;
dp[j].pre=k;
dp[j].now=a[j].speed;
}
}
}
}
int ans=;
int m=i-;
for(int j=;j<=i-;j++)
{
if(ans<dp[j].cnt)
{
ans=dp[j].cnt;
m=j;
}
} cout <<ans<<endl; stack <int> s;
s.push(a[m].num);ans--;
while(ans--)
{
s.push(a[dp[m].pre].num);
m=dp[m].pre;
}
while(!s.empty())
{
cout << s.top()<<endl;
s.pop();
}
return ;
}
J - FatMouse's Speed的更多相关文章
- J - FatMouse's Speed dp
题目链接: https://vjudge.net/contest/68966#problem/J 找最长子串并且记录路径. TLE代码: #include<iostream> #inclu ...
- FatMouse's Speed——J
J. FatMouse's Speed FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- FatMouse's Speed
J - FatMouse's Speed DP的题写得多了慢慢也有了思路,虽然也还只是很简单的DP. 因为需要输出所有选择的老鼠,所以刚开始的时候想利用状态压缩来储存所选择的老鼠,后面才发现n太大1& ...
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- FatMouse's Speed(HDU LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- FatMouse's Speed 基础DP
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- HDU 1160:FatMouse's Speed(LIS+记录路径)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 使用AndroidStudio导入github项目
1.在studio中配置github的项目地址 2.当你点击github,会这个样子 3.此处放你要clone的地址 ,然后点击clone. 4.等一会会出现这个页面,然后点击yes ,会出现这个页面 ...
- Vmware tools install
Vmware tools 1◆ 下载 2◆ diagram Ifcfg-eth0 =====>关闭防火墙 systemctl stop firewalld.service ===== ...
- Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)
项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...
- Mysql 用户ip访问根据省份查询
表名:shop_interview_customer 表结构:customerId空为游客模式 interviedId customerId interviewIP iPdetail 1 1001 1 ...
- 【SQL】group by 及 having
Group By 分组汇总 HAVING:给分组设置条件 1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”, ...
- learning hdmi edid protocol
referenc: https://en.wikipedia.org/wiki/Extended_Display_Identification_Data
- liunx 随笔集
Linux 安装时 Customize Now(自定义选包)选包如下 base system -> base , compatibility libraries,debugging Tool ...
- QuickStart系列:docker部署之MariaDB
Centos7里面没有Mysql 取而代之的是MariaDB,MariaDB是完全开源的.MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的 ...
- ssd物体检测模型训练和测试总结
参考网址:github:https://github.com/naisy/realtime_object_detection 2018.10.16ssd物体检测总结:切记粗略地看一遍备注就开始训练模型 ...
- java倒计时使用ScheduledExecutor实现,使用两个线程,以秒为单位
public class Countdown2 { private volatile int lin; private int curSec; public Countdown2(int lin) t ...