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) ...
随机推荐
- Java 正则校验整数,且小数点只能是2位
//金额验证 public static boolean isNumber(String str){ Pattern pattern=Pattern.compile("^(([1-9]{1} ...
- firstchild.data与childNodes[0].nodeValue意思
x.firstchild.data:获取元素第一个子节点的数据: x.childNodes[0]::获取元素第一个子节点; x.childNodes[0].nodeValue.:也是获取元素第一个子节 ...
- datatime 模块
import datetime # 这个是一个包 里面包含 对时间的处理 对日期的处理datetime.date # 日期相关datetime.time # 时间相关 # 获取当前详细时间print( ...
- memset详解 设置无穷大INF
memest原型 (please type "man memset" in your shell) void *memset(void *s, int c, size_t n); ...
- Cracking The Coding Interview5.1
//You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set a ...
- 6.4 C++提取子字符串及字符串的比较
参考:http://www.weixueyuan.net/view/6393.html 总结: 函数substr可以提取string字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串 ...
- 《图解TCP/IP》读书笔记(转)
reference: https://www.cnblogs.com/edisonchou/p/5987827.html 一.国际惯例:书托 这是一本图文并茂的网络管理技术书籍,旨在让广大读者理解TC ...
- Fedora的一些个人配置
0,老传统 yum install screenfetch 1,关闭蜂鸣器 edit /etc/bashrc setterm -blength 0#setterm -bfreq 10 #这个可以设置声 ...
- CodeForces - 429A Xor-tree
Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, c ...
- 杭电多校第三场 A Ascending Rating
Problem Description Before the start of contest, there are n ICPC contestants waiting in a long queu ...