uva 10131 Is Bigger Smarter?

来源:互联网 发布:centos 搜狗输入法 编辑:程序博客网 时间:2024/04/19 16:03

先将原数组按weight上升的顺序排列,weight相同则按S值下降的顺序,然后针对S值

    d[i]为以i开头的S下降序列的最大长度,  d[i] = max(d[j]) + 1(Sj < Si 且 j > i);

   front 用来保存路径

 

#include <cstdio>#include <cstdlib>#include <algorithm> #include <cstring> using namespace std;#define N 1002 const int inf = 100000000;  struct  elep{         int w, s; }; elep a[N]; int ind[N]; int d[N], front[N];   bool cmp(int x, int y) {       if (a[x].w != a[y].w)return a[x].w < a[y].w;       return a[x].s > a[y].s; }   int main() {       int ww, ss;       int n = 0;     //  FILE* fp = fopen("in.txt", "r");        while (scanf( "%d", &ww) != EOF)       {             scanf( "%d", &ss);             a[n].w = ww;             a[n].s = ss;             ind[n] = n;              n++;       }       sort(ind, ind + n, cmp);       memset(front, -1, sizeof(front));        for (int i = 0; i < n; i++)d[i] = 1;        int maxans = 1;        int ansid = n - 1;        for (int i = n - 2; i >= 0; i--)       {            int max = 0;            int tmpid = -1;              for (int j = i + 1; j < n; j++)                   if(a[ind[i]].w < a[ind[j]].w && a[ind[i]].s > a[ind[j]].s && d[j] > max)                     max = d[j], tmpid = j;            d[i] = max + 1;             front[i] = tmpid;             if (maxans < d[i])            {                 maxans = d[i];                  ansid = i;            }        }        printf("%d\n", maxans);       for (int i = ansid; i != -1; i = front[i])          printf("%d\n", ind[i] + 1);      // getchar();       return 0;  }                                              


 

原创粉丝点击