FatMouse's Speed ~(基础DP)打印路径的上升子序列
InputInput contains data for a bunch of mice, one mouse per line, terminated by end of file.
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.
OutputYour program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that
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.
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7 题意是给你一串数据老鼠的体重和速度,
要你找到一组最长的数组证明体重越大速度越小
这个非常简单就是和求一个最长的上升子序列差不多
这题还有一个要求是打印路径。
所以就要继续前一个数据的下标。
这题细节方面好恶心,
打印路径的时候,只能按照体重最小打印到体重最大的顺序
不能从体重最大到体重最小。(因为这个WA了无数遍)
还有一个恶心的点是数据读入的时候,不知道如何终止数据的读入。
这里需要用的文件的操作,第一次遇到这种题目,表示真心恶心。
一个水题,弄成这样坑爹的题目也不容易
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
using namespace std; const int mod = 1e9 + ;
const int maxn = 1e4 + ;
struct node {
int w, f, id;
} a[maxn] ;
struct ndoe1 {
int num, pre;
} dp[maxn];
int cmp(node a, node b) {
return a.w < b.w;
}
int path[maxn];
int main() {
int k = ;
while(scanf("%d%d", &a[k].w, &a[k].f) != EOF ) {
a[k].id = k + ;
dp[k].num = ;
dp[k].pre = ;
k++;
}
sort(a, a + k, cmp );
int ans = -, temp = ;
for (int i = ; i < k ; i++ ) {
for (int j = ; j < i ; j++) {
if (a[i].w > a[j].w && a[i].f < a[j].f ) {
if (dp[i].num < dp[j].num + ) {
dp[i].num = dp[j].num + ;
dp[i].pre = j;
}
}
}
if (dp[i].num > ans) {
ans = dp[i].num;
temp = i;
}
}
printf("%d\n", ans);
for (int i = ; i < ans ; i++ ) {
path[i] = temp;
temp = dp[temp].pre;
}
for (int i = ans - ; i >= ; i-- ) {
printf("%d\n", a[path[i]].id);
}
return ;
}
FatMouse's Speed ~(基础DP)打印路径的上升子序列的更多相关文章
- 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 ...
- 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) ...
- HDU 1160:FatMouse's Speed(LIS+记录路径)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【noi 2.6_2000】&【poj 2127】 最长公共子上升序列 (DP+打印路径)
由于noi OJ上没有Special Judge,所以我是没有在这上面AC的.但是在POJ上A了. 题意如标题. 解法:f[i][j]表示a串前i个和b串前j个且包含b[j]的最长公共上升子序列长度 ...
- 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 ...
- [HDOJ1160]FatMouse's Speed(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse believes that the fatter a mouse is, th ...
随机推荐
- Problem : 1008 ( Elevator )
好操蛋啊,电梯竟然能原地不动,你大爷的,这逻辑,太弱智了.... Problem : 1008 ( Elevator ) Judge Status : Accepted RunId : 103 ...
- 使用BigDecimal报的错
错误:java.lang.arithmeticexception: non-terminating decimal expansion; no exact representa 小数位膨胀 解决方法 ...
- Python快速入门之与C语言异同
代码较长,建议使用电脑阅读本文. 10分钟入门Python 本文中使用的是Python3如果你曾经学过C语言,阅读此文,相信你能迅速发现这两种语言的异同,达到快速入门的目的.下面将开始介绍它们的异同. ...
- Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录
1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...
- server.xml 解析
server.xml的结构: 常用配置的参数注释: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apach ...
- Navicat通过跳板机连接数据库
完成对应设置后,即可连接数据库,本人亲测!
- 移动端HTML5性能优化
移动端HTML5性能优化 [导读] 得益于智能手机的普及和各行各业互联网+的运动,移动端的市场占比疯狂增长. 2016年1月发布的2015年电商数据显示,2015年中国移动端网购交易额同比暴涨123 ...
- 想在网上保持匿名?教你用Linux如何实现!
想在网上保持匿名?教你用Linux如何实现! 信息时代给我们的生活带来极大便利和好处的同时也带来了很大的风险.一方面,人们只要点击几下按钮,就能基本上访问已知存在的全部信息和知识:另一方面,要是这种权 ...
- 深入java虚拟机学习 -- 内存管理机制
前面说过了类的加载机制,里面讲到了类的初始化中时用到了一部分内存管理的知识,这里让我们来看下Java虚拟机是如何管理内存的. 先让我们来看张图 有些文章中对线程隔离区还称之为线程独占区,其实是一个意思 ...
- CXF SOAP 及其安全控制
通过上一篇文章,相信您已经学会了如何使用 CXF 开发基于 SOAP 的 WS 了.或许您目前对于底层原理性的东西还不太理解,心中难免会有些疑问: 什么是 WSDL? 什么是 SOAP? 如何能让 S ...