首页 > 其他 > 详细

FatMouse's Speed

时间:2020-02-16 23:17:09      阅读:63      评论:0      收藏:0      [点我收藏+]
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

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

Your 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

这道题排序后就是一个最长上升子序列的题,注意的是dp的条件加入被排序的值的比较

输出路径是用一个数组,数组代表当前下标的前一位的值,也就是用数组模拟链表其实直接用链表也行(笑)

最后再用一个栈来完成反向输出路径

注意的地方就是排序的时候同重量和同速度其实没有关系,dp的时候筛选就行

注意模拟链表里面存的不是num要a[s.top()].num这样输出

#define _CRT_SECURE_NO_WARNINGS

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;

struct mouse {
    int num;
    int weight;
    int speed;
};

bool fun(mouse a, mouse b) {
    return a.weight < b.weight;
}

int main() {
    int n = 0;
    mouse a[1001];
    while (~scanf("%d%d", &a[n].weight, &a[n].speed)) {
        
        a[n].num = n + 1;
        n++;
        if (n == 9) {
            break;
        }
    }
    sort(a, a + n, fun);
    int temp[1001];
    for (int i = 0; i < n; i++) {
        temp[i] = i;
    }
    int dp[1001] = { 0 };
    int max = 0;
    int end = 0;
    for (int i = 0; i < n; i++) {
        dp[i] = 1;
        for (int j = 0; j < i; j++) {
            if (dp[j] + 1 > dp[i] && a[j].weight<a[i].weight && a[j].speed>a[i].speed) {
                temp[i] = j;
                dp[i] = dp[j] + 1;
            }
        }
        if (max < dp[i]) {
            max = dp[i];
            end = i;
        }
    }

    cout << max << endl;
    stack<int> s;
    while (max--) {
        s.push(end);
        end = temp[end];
    }
    while (!s.empty()) {
        cout << a[s.top()].num << endl;
        s.pop();
    }


    return 0;
}

 

FatMouse's Speed

原文:https://www.cnblogs.com/Vetsama/p/12319156.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!