HDU - 1160 FatMouse's Speed (dp + 最长上升子序列变形)

                                   FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24549    Accepted Submission(s): 10879
Special Judge

Problem Description

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


题意:给出一堆老鼠(最多1000个),它们有其各自的速度和质量。要求找到 N 个老鼠,要求满足老鼠的质量越大,速度越小,且 N 越大越好。并且按顺序输出这 N 个老鼠的编号。

思路:用结构体数组保存老鼠的速度质量和编号。排序:首先按照“质量大的在前”排序,然后按照“速度小的在前”排序。然后就跟最长上升子序列类似,只不过该题的限制是,后面的老鼠的质量要严格小于前面老鼠的质量,且后面老鼠的速度要严格大于前面老鼠的速度。满足 N 的集合可能有很多,题目说输出任意一个即可,dfs 一下就好了,具体看代码。

//#include <bits/stdc++.h>
#include <map>
//#include <tr1/unordered_map>
#include<limits>
#include <float.h>
#include <list>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
inline int lowbit(int x){ return x & (-x); }
inline int read(){int X = 0, w = 0; char ch = 0;while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();return w ? -X : X;}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x)
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x)
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define db double
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 10;
const int maxp = 1e3 + 10;
const LL INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 20090717;

int dp[1010], ans, pos, w, s;
int ANS[1010];

struct node{
    int W, S, id; //质量、速度、编号
    bool operator<(const node& rhs) const{
        if (W == rhs.W) return S > rhs.S;
        return W < rhs.W;
    }
}mice[1010];

void dfs(int p, int cur){
    ANS[cur] = p;
    if (p == 0) return;
    _For (i, p - 1, 0)
        if (dp[i] == cur - 1){
            dfs(i, cur - 1);
            break;
        }
}

int main(){
    int n = 0;
    while (~Sca2(w, s)){ //读入数据
        ++n;
        mice[n].W = w; //质量
        mice[n].S = s; //速度
        mice[n].id = n; //编号 ( 1 --> n )
    }
    sort(mice + 1, mice + 1 + n); //排序
    For (i, 1, n) dp[i] = 1; //初始化
    For (i, 2, n)
    For (j, 1, i - 1)
        if (mice[i].W > mice[j].W && mice[i].S < mice[j].S) //严格满足条件
            dp[i] = max(dp[i], dp[j] + 1);
    For (i, 1, n)
        if (dp[i] > ans)
            ans = dp[i], pos = i; //找出最大的答案,pos记录的是该集合中质量最小,速度最大的老鼠的排序后标号
    dfs(pos, ans); //递归找出答案,找出的都是排序后老鼠的编号,放在ANS[ ] 数组中
    Pri(ans);
    For (i, 1, ans)
        Pri(mice[ANS[i]].id); //还原标号

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值