zoukankan      html  css  js  c++  java
  • HDU 1160 FatMouse's Speed (DP)

    FatMouse's Speed
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
     
    思路:二次排序后求LIS
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #define    MAX_SIZE    1005
     5 
     6 struct    x
     7 {
     8     int    weight;
     9     int    speed;
    10     int    num,sum,front;
    11 }DP[MAX_SIZE];
    12 
    13 int    comp(const void * a,const void * b);
    14 int    main(void)
    15 {
    16     int    i = 0;
    17     int    max,max_loc,ans,ans_loc;
    18     int    box[MAX_SIZE];
    19 
    20     while(scanf("%d%d",&DP[i].weight,&DP[i].speed) != EOF)
    21     {
    22         DP[i].num = i;
    23         DP[i].sum = 1;
    24         DP[i].front = -1;
    25         i ++;
    26     }
    27 
    28     qsort(DP,i,sizeof(struct x),comp);
    29 
    30     ans_loc = 0;
    31     ans = 0;
    32     for(int j = 1;j < i;j ++)
    33     {
    34         max = 0;
    35         for(int k = 0;k < j;k ++)
    36             if(DP[j].weight > DP[k].weight && DP[j].speed < DP[k].speed)
    37                 if(DP[k].sum > max)
    38                 {
    39                     max = DP[k].sum;
    40                     max_loc = k;
    41                 }
    42 
    43         DP[j].sum += max;
    44         if(max)
    45             DP[j].front = max_loc;
    46 
    47         if(ans < DP[j].sum)
    48         {
    49             ans = DP[j].sum;
    50             ans_loc = j;
    51         }
    52     }
    53 
    54     box[0] = ans_loc;
    55     i = 0;
    56     while(box[i] != -1)
    57     {
    58         i ++;
    59         box[i] = DP[box[i - 1]].front;
    60     }
    61 
    62     printf("%d
    ",ans);
    63     for(i --;i >= 0;i --)
    64         printf("%d
    ",DP[box[i]].num + 1);
    65 
    66     return    0;
    67 }
    68 
    69 int    comp(const void * a,const void * b)
    70 {
    71     if((*(struct x *)a).weight == (*(struct x *)b).weight)
    72         return    (*(struct x *)b).speed - (*(struct x *)a).speed;
    73     return    (*(struct x *)a).weight - (*(struct x *)b).weight;
    74 }
  • 相关阅读:
    O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)
    北京网选赛第二题(最大仰望角度)
    最小圆覆盖(随机增量法&模拟退火法)
    模拟退火算法A Star not a Tree?(poj2420)
    模拟退火算法(run away poj1379)
    模拟退火算法(西安网选赛hdu5017)
    最小费用流判负环消圈算法(poj2175)
    中国邮递员问题(一)
    破坏行动问题
    进化树问题
  • 原文地址:https://www.cnblogs.com/xz816111/p/4180342.html
Copyright © 2011-2022 走看看