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 ...
随机推荐
- C++中 #include<>与#include""
#include<> 使用尖括号表示在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找: #include"" 使用双引号则表示首先在 ...
- windows中安装redis
Redis是有名的NoSql数据库,一般Linux都会默认支持.但在Windows环境中, Windows的Redis安装包需要到以下GitHub链接找到.链接:https://github.com/ ...
- Linux环境下jdk1.8压缩包下载
jdk1.8下载: 百度云链接:https://pan.baidu.com/s/1c37VcPi 密码:e6qh
- python 对象和json互相转换
一.python对json的支持 从python2.6开始,python标准库中添加了对json的支持,操作json时,只需要import json即可. 二.python对象转换成json字符串 在 ...
- [总结] O(n)求和为0的最长子段
以这题为例 Solution 我们首先用前缀和差分,可以认为G是1,R是-1,然后求一个前缀和qzh. 如果 qzh[i]==qzh[j] 那么 i~j 这一整段,一定是一个和为0的区间,即红绿相等的 ...
- 搭建nuxtjs程序 —— 用户信息 or token怎么不丢失
框架背景:开发框架采用vue,需要更好的SEO,更快的内容到达时间,从浏览器看不到对服务器的请求接口,选用开箱即用的nuxtjs. 问题背景:1. 前后分离,需前端存储token及登录后的用户信息: ...
- RACSignal的一些常用用法
NSData + RACSupport.h @interface NSData (RACSupport) // Read the data at the URL using -[NSData init ...
- Algorithm --> 求1到n的和
求1到n的和 输入n,求和1到n,要求不能使用乘除法,不使用任何if while for 以及三目运算,怎么做? 版本一 static int f(int n) { n && (n + ...
- ListView动态刷新adapter.notifyDataSetChanged()无反应
前段时间在做一个动态刷新ListView(模拟聊天),遇到一个问题,调用notifyDataSetChanged()方法,数据源已经存在但是并没有动态刷新! 首先我们需要了解notifyDataSet ...
- C语言描述队列的实现及操作(链表实现)
// 队列的单链表实现 // 头节点:哨兵作用,不存放数据,用来初始化队列时使队头队尾指向的地方 // 首节点:头节点后第一个节点,存放数据 #include<stdio.h> #incl ...