您的位置:首页 > 其它

hdu 1160

2016-05-28 20:30 232 查看
这个题目简单 但是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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: