zoukankan      html  css  js  c++  java
  • 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed

    https://vjudge.net/contest/68966#problem/J

    【Accepted】

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<cmath>
     6 #include<algorithm>
     7 
     8 using namespace std;
     9 const int maxn=1e3+3;
    10 struct node
    11 {
    12     int w;
    13     int s;
    14     int id;
    15 }a[maxn];
    16 int pre[maxn];
    17 int dp[maxn];
    18 bool cmp(node x,node y)
    19 {
    20     return x.w<y.w;
    21 }
    22 
    23 void Print(int pos)
    24 {
    25     if(pos==-1) return;
    26     Print(pre[pos]);
    27     printf("%d
    ",a[pos].id);
    28 }
    29 int main()
    30 {
    31     int cnt=0;
    32     while(scanf("%d%d",&a[cnt].w,&a[cnt].s)!=EOF)
    33     {
    34         a[cnt].id=cnt+1;
    35         cnt++; 
    36     }
    37     sort(a,a+cnt,cmp); 
    38     memset(pre,-1,sizeof(pre));
    39     fill(dp,dp+cnt,1);
    40     for(int i=0;i<cnt;i++)
    41     {
    42         for(int j=0;j<i;j++)
    43         {
    44             if(a[j].w<a[i].w&&a[j].s>a[i].s)
    45             {
    46                 if(dp[j]+1>dp[i])
    47                 {
    48                     dp[i]=dp[j]+1;
    49                     pre[i]=j;
    50                 }
    51             }
    52         }
    53     }
    54     int cou=1;
    55     int pos=-1;
    56     for(int i=0;i<cnt;i++)
    57     { 
    58         if(dp[i]>cou)
    59         {
    60             cou=dp[i];
    61             pos=i;
    62         }
    63     }
    64     if(cou==1)
    65     {
    66         printf("%d %d
    ",1,1);
    67         return 0;
    68     }
    69     cout<<cou<<endl;
    70     Print(pos);
    71     return 0;    
    72 } 
    View Code

    【教训】

    while(scanf("%d%d",&a[cnt].w,&a[cnt].s)!=EOF)
        {
            a[cnt].id=++cnt;
        }

    是不对的,先算++cnt,前面的cnt已经加一了

  • 相关阅读:
    Tarjan 的一些板子
    对 SAM 和 PAM 的一点理解
    一些敲可爱的数论板子
    异常
    面向对象编程
    JAVA数组
    JAVA方法
    JAVA流程控制
    JAVA基础
    JAVA入门
  • 原文地址:https://www.cnblogs.com/itcsl/p/7225732.html
Copyright © 2011-2022 走看看