hdu 1160

这个题目简单 但是wa了很久 一直找不到自己错在哪里 最后对比了一下别人代码 终于发现原来dp要初始化为1  以后要细心!
这个题目数据好水的说 看了网上很多没有进行二级排序的都过了 其实这个地方应该对相同的weight进行二级排序
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <sstream>
#include <ostream>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#define pi acos(-1)
#define e exp(1)
#define inf 0x3f3f3f3f
using namespace std;
struct node{
    int weight;int speed;int num;
};
int cmp(node s1,node s2) {if(s1.weight!=s2.weight) return s1.weight<s2.weight;return s1.speed>s2.speed;}
node arr[1005];
int dp[1005];
int save[1005];
int main()
{
    int a,b;
    int i=0;
    while(scanf("%d %d",&a,&b)!=EOF)
    {
        arr[i].weight=a;arr[i].speed=b;arr[i].num=i+1;
        ++i;
    }
    sort(arr,arr+i,cmp);
    memset(save,-1,sizeof(save));
    dp[1]=1;int num=0;
    for(int j=1;j<i;j++)
    {
        dp[j]=1;
        for(int k=0;k<j;k++)
            if(arr[k].weight!=arr[j].weight&&arr[k].speed>arr[j].speed&&dp[k]+1>dp[j]) {dp[j]=dp[k]+1;save[j]=k;}
    }
    int big=0;
    for(int j=1;j<i;j++) if(dp[j]>big) {big=dp[j];num=j;}
    cout<<big<<endl;
    stack<int >s;
    while(num!=-1) {s.push(num);num=save[num];}
    while(!s.empty()) {cout<<arr[s.top()].num<<endl;s.pop();}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值