uva10131

来源:互联网 发布:支票打印软件破解版 编辑:程序博客网 时间:2024/06/08 17:55

题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输出最长值和方案, 方案不唯一的时候任意输出一种。

思路:
将重量从小到大进行排序,按智商大到小最一个最长递增子序列

代码:

#include <iostream>using namespace std;#include <cstring>#include <stdio.h>#include <algorithm>const int maxn = 10005;struct e{    int weight;    int smart;    int flag;}el[maxn];int b[maxn];int father[maxn];void print(int end) {    if(father[end] == 0) {        printf("%d\n",el[end].flag);        return;    }    print(father[end]);    printf("%d\n",el[end].flag);}int cmp(const void * a,const void *b) {    struct e * c= (e *) a;    struct e * d = (e *)b;    return c->weight - d-> weight;    //return (el *)a->weight - (el *)b->weight;}int main() {    int p = 1;    while(~scanf("%d %d",&el[p].weight,&el[p].smart)) {        b[p] = 1;        el[p].flag = p;        p++;    }    p--;    qsort(el + 1,p,sizeof(e),cmp);    int len = 0, end = 0;    for(int i = 1; i<= p; i++) {        for(int j = 1; j < i; j++) {            if(el[j].weight < el[i].weight && el[j].smart > el[i].smart && b[j] + 1 > b[i]) {                b[i] = b[j] + 1;                father[i] = j;            }            if(b[i] > len) {                len = b[i];                end = i;            }        }    }    printf("%d\n",len);    print(end);    return 0;}
0 0
原创粉丝点击