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) ...
随机推荐
- 通过springboot 去创建和提交一个表单(七)
创建工程 涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖. 1 2 3 4 5 6 7 8 9 10 11 1 ...
- AI工具(星形工具)(光晕工具)(移动复制)(柜子绘制)5.12
星形工具;基本操作与矩形一样,拖动星形工具绘制,点击键盘上箭头增加星形的角数.下箭头减少星形的角数. 选择星形工具在屏幕单击,出现星形对话框,可以设置半径1半径2,角点数.图中的星形就可以用星形工具绘 ...
- 阿里云免费申请https证书
申请地址 https://common-buy.aliyun.com/?spm=a2c4e.11153940.blogcont65199.22.30f968210RsUSx&commodi ...
- learning ddr mode register MR3
- learning at command AT+CIMI
AT command AT+CIMI [Purpose] Learning how to get the International Mobile Subscriber Identity ...
- 遍历所有子物体中renderer(渲染器)中的material(材质)
//得到所有可渲染的子物体Renderer[] rds = transform.GetComponentsInChildren<Renderer>();//逐一遍历他的子物体中的Rende ...
- iframe子父页面函数互相调用
1.iframe子页面调用父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: window.parent.a(); 子页面取父页面中的标签 ...
- js两种打开新窗口
1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a> 等效于js代码 ...
- HTML5 ①
<!DOCTYPE html> <!--声明当前页面是H5--> html框架: <html> <head lang="en"> & ...
- oracle查看编码以及修改编码
oracle的编码一直是个很重要的问题,以前也总结的写过,但都忘了,今天再在这写一下. 首先查看oracle数据库的编码 SQL>select * from nls_database_param ...