zoj 1108 FatMouse's Speed 基础dp
Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input Specification
Input 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.
Output Specification
Your program should output a sequence of lines of data; the first line should contain a numbern; the remainingn lines should each contain a single positive integer (each one representing a mouse). If thesen integers arem[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
Output for Sample Input
4
4
5
9
7
天啊,好可耐的老鼠,抱抱。
为达到DP的无后续性,以其中一个为关键词排序,然后最长上升subque。
从后向前不需要倒序输出。从前向后不要忘记倒序输出:
用 数组记录或者递归实现。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int m,x,y;
struct in{
int w,s,pos,pre,num;
}a[];
int b[];
bool cmp(in a,in b){
return a.w<b.w;
}
void _solve()
{
for(int i=;i<=m;i++){
for(int j=;j<i;j++){
if(a[i].w>a[j].w&&a[i].s<a[j].s)
if(a[i].num<a[j].num+){
a[i].num=a[j].num+;
a[i].pre=j;
}
}
}
int M=,L=;
for(int i=m;i>=;i--)
{
if(a[i].num>M) {
M=a[i].num;
L=i;
}
}
printf("%d\n",M);
int t=;
while(L>){//也可以试试调用递归来倒序输出
b[++t]=a[L].pos;
L=a[L].pre;
}
for(int i=t;i>=;i--) printf("%d\n",b[i]);
return ;
}
int main()
{
while(~scanf("%d%d",&x,&y))
{
a[++m].w=x;
a[m].s=y;
a[m].pos=m;
a[m].num=;
}
sort(a+,a+m+,cmp);
_solve();
return ;
}
zoj 1108 FatMouse's Speed 基础dp的更多相关文章
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- 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 (HDU 1160) DP
传送门: ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=108 HDU :http://acm.hdu.edu.cn/s ...
- [HDOJ1160]FatMouse's Speed(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse believes that the fatter a mouse is, th ...
- HDU 1160 FatMouse's Speed(DP)
点我看题目 题意 :给你好多只老鼠的体重和速度,第 i 行代表着第 i 个位置上的老鼠,让你找出体重越大速度越慢的老鼠,先输出个数,再输出位置. 思路 :看题的时候竟然脑子抽风了,看了好久愣是没明白题 ...
- HDU 1160 FatMouse's Speed LIS DP
http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...
- hdu FatMouse's Speed 动态规划DP
动态规划的解决方法是找到动态转移方程. 题目地址:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3§ionid ...
- HDU 1160 FatMouse's Speed (sort + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...
- HDU FatMouse's Speed 基本DP
题意:要求找到的体重递增,速度递减的老鼠,并且输出最长的长度数,而且输出各自的序列数.Special Judge 思路:先按体重由小到大排序,再找最长速度递减序列. 转移方程:mou[i].w> ...
随机推荐
- 语言小知识-Java ArrayList类 深度解析
· 问题 1:ArrayList 的 size 和 capacity 怎么理解? 如果把 ArrayList 看作一个杯子的话,capacity 就是杯子的容积,也就是代表杯子能装多少东西,而 siz ...
- Android scrollview和GridView混合使用
在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全.解决办法,自定义一个GridView控件 p ...
- 为什么HikariCP被号称为性能最好的Java数据库连接池,如何配置使用
转自:https://blog.csdn.net/clementad/article/details/46928621 HiKariCP是数据库连接池的一个后起之秀,号称性能最好,可以完美地PK掉其他 ...
- 雷林鹏分享:Ruby 循环
Ruby 循环 Ruby 中的循环用于执行相同的代码块若干次.本章节将详细介绍 Ruby 支持的所有循环语句. Ruby while 语句 语法 while conditional [do] code ...
- 女生学java是否真的没有优势
随着女性越来越独立,我们可以看到再以前我们认为不适合女性朋友从事的工作,也出现了越来越多的女生,例如对IT行业也不再跟之前一样畏惧.虽然当下很多人所持的观点依旧是,女生不适合IT行业,但是很多女生已经 ...
- 20170708xlVBA添加新产品修改公式
Sub ControlInsertProduct() Dim Wb As Workbook Dim OneSht As Worksheet Dim Arr As Variant Dim i As Lo ...
- (GoRails )使用Vue.js制作拖拉list功能(v1-4) gem 'acts_as_list'(自动排列顺序)
系列视频: use Vue.js to build the drag and drop support for the list themselves the cards that are under ...
- Jersey 2.x 服务器端应用支持的容器
基于 JAX-RS Servlet-based 部署的一部分标准,能运行在任何支持 Servlet 2.5 和更高标准的的容器上.Jersey 提供支持程序化部署在下面的容器中:Grizzly 2 ( ...
- 55. 45. Jump Game II *HARD*
1. Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- laravel 连接同一服务器上多个数据库操作 、 连接多个不同服务器上的不同数据库操作以及多个数据库操作的事务处理
!注意:标红的要注意区分开 第一步.配置.env文件(同一服务器上多个数据库) DB_CONNECTION=pgsqlDB_HOST=IP(例如:127.0.0.1)DB_PORT=端口号(例如:54 ...