【DP】UVA 10131 Is Bigger Smarter? LIS

来源:互联网 发布:realtek mac 驱动 编辑:程序博客网 时间:2024/06/05 08:14

排序后dp

写代码的速度还不够快。。。

</pre><pre name="code" class="cpp">#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <string>#include <iostream>#include <algorithm>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#define cler(arr, val)    memset(arr, val, sizeof(arr))#define IN     freopen ("in.txt" , "r" , stdin);#define OUT  freopen ("out.txt" , "w" , stdout);typedef long long  LL;const int MAXN = 111;//点数的最大值const int MAXM = 20006;//边数的最大值const int INF = 11521204;const int mod=1000000007;struct node{    int x,y,num;}a[1999];bool cmp1(node h,node w){    if(h.x==w.x)        return h.y>w.y;    return h.x<w.x;}int dp[1111],out[1111];int print(int x){    if(out[x]!=0)        print(out[x]);    printf("%d\n",a[x].num);}int main(){    int n=1;  //  IN;    while(scanf("%d%d",&a[n].x,&a[n].y)!=EOF)    {        a[n].num=n;        n++;    }    cler(out,0);    sort(a+1,a+n+1,cmp1);    for(int i=1;i<n;i++)    {        dp[i]=1;       // printf("%d %d %d\n",a[i].x,a[i].y,a[i].num);        for(int j=1;j<i;j++)        {            if(a[i].x>a[j].x&&a[i].y<a[j].y&&dp[j]+1>dp[i])            {                dp[i]=dp[j]+1;                out[i]=j;            }        }    }    int ans=0,p=0;    for(int i=1;i<n;i++)    {        if(dp[i]>ans)            ans=dp[i],p=i;    }    printf("%d\n",ans);    print(p);    return 0;}

0 0
原创粉丝点击