首次完全独立写DP,其他的的相关题解全部是按照先体重排序然后最长递减子序列,自己就改下
#include <map>
#include <set>
#include <list>
#include <cmath>
#include<cctype>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b)
{
return a % b == 0 ? b : gcd(b, a % b);
}
#define MAXN 1010
int cas;
int dp[MAXN];
int Map[MAXN];
int fa[MAXN];
struct node
{
int weight;
int iq;
int index;
friend bool operator < (const node&a,const node&b)
{
if (a.iq!=b.iq) return a.iq>b.iq;
return a.weight<b.weight;
}
}src[MAXN];
void print(int s)
{
if (fa[s]!=-1)
print(fa[s]);
printf("%d\n",src[s].index);
}
void show()
{
for (int i=0;i<cas;i++)
printf("%d %d\n",src[i].iq,src[i].weight);
}
int main()
{
//freopen("sample.txt","r",stdin);
cas=0;
while (scanf("%d%d",&src[cas].weight,&src[cas].iq)!=EOF)
{
src[cas].index=cas+1;cas++;
}
//printf("%d\n",cas);
for (int i=0;i<cas;i++)
{
dp[i]=1;
fa[i]=-1;
}
sort(src,src+cas);
//show();
int ans=-1,s=-1;
//定义状态dp[i]为以第i个元素为结尾的最长递增子序列
for (int i=1;i<cas;i++)
{
for (int j=0;j<i;j++)
{
if (src[j].weight<src[i].weight && src[j].iq>src[i].iq)
{
if (dp[j]>=dp[i]) {dp[i]=dp[j]+1;fa[i]=j;}//i的父亲是jz
}
}
if (dp[i]>ans)
{
ans=dp[i];
s=i;
}
}
//for (int i=0;i<cas;i++) printf("%d ",dp[i]);
//putchar('\n');
printf("%d\n",ans);
print(s);
return 0;
}