HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14980 Accepted Submission(s): 6618
Special Judge
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[n]]
In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
题目链接:HDU 1160
比较裸的二维LIS,但是WA了无数次, 好像是把id和下标搞混了………………,由于题目说重量weight要递增的情况下speed要递减,显然是先对weight进行排序再按speed关键字找最长严格下降子序列,但这样不符合个人习惯……于是就换了一下按weight递减,speed递增,找最长严格上升子序列,显然当weight相同时要把speed小的排前面,然后记录每一个可能组成LIS的老鼠的前驱,由于顺序是反着找的,最后回溯出来的顺序就是答案了,不用放到栈里,但由于要统计最长的个数,还是放到队列里方面。我这个代码出来的样例答案是 4 4 5 6 1,也是符合题意的
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
struct info
{
int speed;
int weight;
int pre;
int id;
bool operator<(const info &t)const
{
return (weight>t.weight||(weight==t.weight&&speed<t.speed));
}
};
info arr[N];
int dp[N]; int main(void)
{
int w,s,i,j;
int cnt=0;
while (~scanf("%d%d",&w,&s))
{
arr[cnt].id=cnt+1;
arr[cnt].pre=-1;
arr[cnt].weight=w;
arr[cnt].speed=s;
++cnt;
}
sort(arr,arr+cnt);
CLR(dp,0);
for (i=0; i<cnt; ++i)
{
int tpre=-1;
int pre_max=0;
for (j=0; j<i; ++j)
{
if(dp[j]>pre_max&&arr[j].speed<arr[i].speed&&arr[j].weight>arr[i].weight)
{
pre_max=dp[j];
tpre=j;
}
dp[i]=pre_max+1;
arr[i].pre=tpre;
}
}
int indx=max_element(dp,dp+cnt)-dp;
queue<int>pos;
while (indx!=-1)
{
pos.push(arr[indx].id);
indx=arr[indx].pre;
}
printf("%d\n",pos.size());
while (!pos.empty())
{
printf("%d\n",pos.front());
pos.pop();
}
return 0;
}
HDU 1160 FatMouse's Speed(要记录路径的二维LIS)的更多相关文章
- HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形
题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...
- HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化
HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...
- hdu 1160 FatMouse's Speed(最长不下降子序列+输出路径)
题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- hdu 1160 FatMouse's Speed 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...
随机推荐
- 分享Kali Linux 2016.2第49周虚拟机
分享Kali Linux 2016.2第49周虚拟机该虚拟机使用Kali Linux 2016.2第49周的64位镜像安装而成.基本配置如下:(1)该系统默认设置单CPU双核,内存为2GB,硬盘为50 ...
- yii2.0 的数据的 增
增加数据 /** * 添加数据 controller 层 */ //引入对应的model类 use app\models\About; //定义对应的方法固定的actionxxxx ...
- bzoj1005 [HNOI2008]明明的烦恼
1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3032 Solved: 1209 Description ...
- 配置FastDFS
一.安装 (一)下载FastDFS安装包 FastDFS官方论坛:http://www.csource.org 下载1:http://sourceforge.net/projects/fastdfs/ ...
- 【POJ】3243 Clever Y
http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...
- 【ZOJ】3329 One Person Game
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个色子,分别有k1.k2.k3个面,权值分别是1-k1, 1~ ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- 关于SASS--->推荐使用
作为前端(html.javascript.css)的三大马车之一的css,一直以静态语言存在,HTML5火遍大江南北了.javascript由于NODE.JS而成为目前前后端统一开发语言的不二之选.只 ...
- spring源码学习之路---深入AOP(终)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...
- filter,orderBy等过滤器
filter用法比较灵活(也增加了较高的复杂度),单独列出. 基本的用法: <input type="text" class="search" ng-mo ...