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 ...
随机推荐
- 卸载Photoshop
安装Photoshop后出现卸载不干净的问题,从而导致安装另一个photoshop版本时出现错误,一般情况下我们都是在控制面板的卸载程序里进行卸载的,但往往这样的卸载并不能清除注册表残留以及系统盘里的 ...
- P2P的理解
B2C是Business-to-Customer的缩写,而其中文简称为“商对客”.“商对客”是电子商务的一种模式,也就是通常说的商业零售,直接面向消费者销售产品和服务.这种形式的电子商务一般以网络零售 ...
- outlook——还原“未读邮件”文件夹
参考链接:http://office.microsoft.com/zh-cn/outlook-help/HA010283248.aspx 摘抄: 还原“未读邮件”文件夹 全部显示 全部隐藏 “未读邮件 ...
- POJ 1681 (开关问题+高斯消元法)
题目链接: http://poj.org/problem?id=1681 题目大意:一堆格子,或白或黄.每次可以把一个改变一个格子颜色,其上下左右四个格子颜色也改变.问最后使格子全部变黄,最少需要改变 ...
- Codeforces Round #352 (Div. 2) A Summer Camp
Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard proble ...
- codeforces round #234B(DIV2) B Inna and New Matrix of Candies
#include <iostream> #include <vector> #include <string> #include <algorithm> ...
- CDOJ 1437 谭松松的旅游计划 Label:倍增LCA && 最短路
谭松松的旅游计划 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- Codeforce - Travelling Salesman
After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelli ...
- 【BZOJ】2456: mode
http://www.lydsy.com/JudgeOnline/problem.php?id=2456 题意:给一个$n<=500000$的数列,求出现次数超过$\lfloor \frac{n ...
- 【BZOJ】3434: [Wc2014]时空穿梭
http://www.lydsy.com/JudgeOnline/problem.php?id=3434 题意:n维坐标中要找c个点使得c个点在一条线上且每一维的坐标单调递增且不能超过每一维限定的值m ...