FatMouse's Speed
TimeLimit: 2Seconds Memory Limit: 65536KB Special Judge
FatMousebelieves that the fatter a mouse is, the faster it runs. To disprove this, youwant to take the data on a collection of mice and put as large a subset of thisdata as possible into a sequence so that the weights are increasing, but thespeeds are decreasing.
InputSpecification
Inputcontains data for a bunch of mice, one mouse per line, terminated by end offile.
Thedata for a particular mouse will consist of a pair of integers: the firstrepresenting its size in grams and the second representing its speed incentimeters per second. Both integers are between 1 and 10000. The data in eachtest case will contain information for at most 1000 mice.
Twomice may have the same weight, the same speed, or even the same weight andspeed.
OutputSpecification
Yourprogram should output a sequence of lines of data; the first line shouldcontain a number n; the remaining n lines should each contain a single positive integer(each one representing a mouse). If these n integersare m[1], m[2],..., m[n] then itmust be the case that
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[n]]
Inorder for the answer to be correct, n should be as large as possible.
Allinequalities are strict: weights must be strictly increasing, and speeds mustbe 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 SampleInput
4
4
5
9
7
【题目大意】:输入n行老鼠,每行老鼠包含体重和速度,要求输出一系列老鼠,使老鼠的重量是严格递增但是速度是严格递减的,找出最大的一个组;
【分析】该题,显然是要用记忆搜索DP,与上一题非常相似,从最重的老鼠开始计算,同时还要记录所有情况的顺序;题目比较简单,不多做解释:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
private ArrayList<Node> al;
private int count;
public Main(){
al = new ArrayList<Node>();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
Main ma = new Main();
int count=0;
while(cin.hasNext()){
ma.al.add(new Node(cin.nextInt(),cin.nextInt(),count));
count ++;
}
ma.count = count;
ma.getMaxSequencePrintIt();
}
private void getMaxSequencePrintIt() {
// TODO Auto-generated method stub
Collections.sort(al);
Node vert;
for(int i=count-1;i>=0;i--){
vert = al.get(i);
for(int j=i+1;j<count;j++){
if(vert.weight == al.get(j).weight){
continue;
}
if(vert.speed > al.get(j).speed){
if(vert.count < (al.get(j).count+1)){
vert.next = al.get(j);
vert.count = al.get(j).count+1;
}
}
}
}
//找出count最大的值
int max,index;
max = 0;
index = 0;
for(int i=0;i<al.size();i++){
if(max < al.get(i).count){
max = al.get(i).count;
index = i;
}
}
System.out.println(max+1);
Node n = al.get(index);
while(n != null){
System.out.println(n.index+1);
n = n.next;
}
}
static class Node implements Comparable<Node> {
private int weight;
private int speed;
private Node next;
private int count;
private int index;
public Node(int weight, int speed,int index) {
this.weight = weight;
this.speed = speed;
count = 0;
next = null;
this.index = index;
}
@Override
public int compareTo(Node o) {
// TODO Auto-generated method stub
if (weight < o.weight) {
return -1;
} else if (weight > o.weight) {
return 1;
}
return 0;
}
}
}