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 ...
随机推荐
- vue使用 better-scroll的参数和方法
格式:var obj = new BScroll(object,{[option1,],.,.}); 注意: 1.要确保object元素的高度比其父元素高 2.使用时,一定要确保object所在的do ...
- 设计模式之观察者(OBSERVER)模式
定义 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. Observer模式描述了如何建立这种关系.这一模式中的关键对象是目标(subject ...
- POJ-1032-拆数字
Description New convocation of The Fool Land's Parliament consists of N delegates. According to the ...
- C#内存泄漏--event内存泄漏
内存泄漏是指:当一块内存被分配后,被丢弃,没有任何实例指针指向这块内存, 并且这块内存不会被GC视为垃圾进行回收.这块内存会一直存在,直到程序退出.C#是托管型代码,其内存的分配和释放都是由CLR负责 ...
- java并发编程基础 --- 4.1线程简介
一.线程简介 什么是线程: 现在操作系统在运行一个程序时,会为其创建一个进程.例如,启动一个java程序,操作系统就会创建一个java进程.现代操作系统调度的最小单元是线程,也叫轻量级进程,在一个进程 ...
- Android 优化APP 构建速度的17条建议
转载:http://www.jianshu.com/p/a1cc8f2e0877 较长的构建时间将会减缓项目的开发进度,特别是对于大型的项目,app的构建时间长则十几分钟,短则几分钟,长的构建时间已经 ...
- js和jquery实现显示隐藏
(选择的重要性) 当点击同一个按钮的时候实现显示影藏 <a id="link" class="b-btn-four task-resolve add-sub-tas ...
- JavaWeb学习笔记三 Servlet
Servlet 是运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API,动态的向客户 ...
- Beta阶段敏捷冲刺报告-DAY2
Beta阶段敏捷冲刺报告-DAY2 Scrum Meeting 敏捷开发日期 2017.11.3 会议时间 13:00 会议地点 微信群 参会人员 项目组全体成员 会议内容 打包问题修复, 爬虫优化, ...
- Beta冲刺NO.1
Beta冲刺 第一天 1. 昨天的困难 由于今天还是第一天,所以暂时没有昨天的困难. 2. 今天解决的进度 潘伟靖: 对代码进行了review 1.将某些硬编码改为软编码 2.合并了一些方法,简化代码 ...