zoukankan      html  css  js  c++  java
  • 【HDOJ】1160 FatMouse's Speed

    DP。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 
     5 #define MAXNUM 1005
     6 
     7 typedef struct {
     8     int w, s;
     9     int index;
    10 } mouse_st;
    11 
    12 mouse_st mouses[MAXNUM];
    13 int dp[MAXNUM], next[MAXNUM];
    14 
    15 int comp(const void *a, const void *b) {
    16     mouse_st *p = (mouse_st *)a;
    17     mouse_st *q = (mouse_st *)b;
    18     if (p->w == q->w)
    19         return p->s - q->s;
    20     else
    21         return q->w - p->w;
    22 }
    23 
    24 int main() {
    25     int n=0, m, beg;
    26     int i, j, max, index;
    27 
    28     while (scanf("%d %d", &mouses[n].w, &mouses[n].s) != EOF) {
    29         mouses[n].index = n;
    30         ++n;
    31     }
    32 
    33     memset(dp, 0, sizeof(dp));
    34     qsort(mouses, n, sizeof(mouse_st), comp);
    35     for (i=0; i<n; ++i)
    36         next[i] = i;
    37     m = 0;
    38 
    39     for (i=0; i<n; ++i) {
    40         max = 0;
    41         for (j=0; j<i; ++j) {
    42             if (mouses[j].w>mouses[i].w && mouses[j].s<mouses[i].s) {
    43                 if (max < dp[j]) {
    44                     max = dp[j];
    45                     index = mouses[j].index;
    46                 }
    47             }
    48         }
    49         if (max)
    50             next[mouses[i].index] = index;
    51         dp[i] = max + 1;
    52         if (m < dp[i]) {
    53             m = dp[i];
    54             beg = mouses[i].index;
    55         }
    56     }
    57     if (m == 1)
    58         printf("1
    1
    ");
    59     else {
    60         printf("%d
    ", m);
    61         while (next[beg] != beg) {
    62             printf("%d
    ", beg+1);
    63             beg = next[beg];
    64         }
    65         printf("%d
    ", beg+1);
    66     }
    67 
    68     return 0;
    69 }
  • 相关阅读:
    与DispatcherServlet的 url-pattern配置问题
    MyBatis ehcache二级缓存
    MyBatis 查询缓存
    【SPOJ283】Naptime
    【洛谷P1858】多人背包
    【洛谷P1486】郁闷的出纳员
    【POJ3017】Cut the Sequence
    【SPOJ10628】Count on a tree
    【洛谷P3224】永无乡 并查集+Splay启发式合并
    【洛谷P2617】Dynamic Rankings
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3704055.html
Copyright © 2011-2022 走看看