码迷,mamicode.com
首页 > 其他好文 > 详细

[DP]做哭了的肥老鼠

时间:2015-08-11 18:29:14      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

FatMouse‘s Speed

TimeLimit: 2000/1000 MS (Java/Others)  MemoryLimit: 65536/32768 K (Java/Others)
64-bit integer IO format:%I64d

 

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. 
SampleInput
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
SampleOutput
4
4
5
9
7

排序后就是最大递增子序列做法,无限WA,后来C++交过了,万恶的G++
#include<iostream>
#include<queue>
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stack>
#include <algorithm>    //STL 通用算法
#include <bitset>     //STL 位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL 双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL 定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL 线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>       //基本输入/输出支持
#include <iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL 队列容器
#include <set>       //STL 集合容器
#include <sstream>     //基于字符串的流
#include <stack>      //STL 堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>    //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL 通用模板类
#include <vector>     //STL 动态数组容器
#include <cwchar>
#include <cwctype>
using namespace std;
typedef struct
{
    int a;
    int b;
    int num;
}node;
node q[10005];
int dp[10005],d[10005];
int findp(int x)
{
    if(d[x]==x)
    {
        printf("%d\n",x);
        return 0;
    }
    findp(d[x]);
    printf("%d\n",x);
    return 0;
}
int cmp (node X,node Y)
{
    if(X.a==Y.a)
        return X.b>Y.b;
    else
        return X.a<Y.a;
}
int main()
{
    int n=0,i,j,maxn,maxx;
    while(scanf("%d %d",&q[n].a,&q[n].b)!=EOF)
    {
        q[n].num=n+1;
        n++;
    }
    sort(q,q+n,cmp);
    maxn=1;
    maxx=1;
    memset(dp,0,sizeof(dp));
    memset(d,0,sizeof(d));
    d[q[0].num]=q[0].num;
    for(i=0; i<n; i++)
    {
        for(j=0; j<i; j++)
        {
            if(q[i].a>q[j].a&&q[i].b<q[j].b)
            {
                if(dp[i]<dp[j])
                {
                    dp[i]=dp[j];
                    d[q[i].num]=q[j].num;
                }
            }
        }
        dp[i]++;
        if(dp[i]==1)
        {
            d[q[i].num]=q[i].num;
        }
        if(dp[i]>maxn)
        {
            maxn=dp[i];
            maxx=q[i].num;
        }
    }
        printf("%d\n",maxn);
        findp(maxx);
    return 0;
}

 

[DP]做哭了的肥老鼠

标签:

原文地址:http://www.cnblogs.com/Qiao994255978/p/4721275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!