最长单调子序列例题

1. HDU 1025  Constructing Roads In JGShining's Kingdom

Problem Description

JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.

Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.

Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II.

The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones.

But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.

For example, the roads in Figure I are forbidden.
 



In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^

 

 

Input

Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.

 

 

Output

For each test case, output the result in the form of sample.
You should tell JGShining what's the maximal number of road(s) can be built.

 

 

Sample Input

 

2

1 2

2 1

3

1 2

2 3

3 1

Sample Output

Case 1: My king, at most 1 road can be built.

Case 2: My king, at most 2 roads can be built.

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=5*1e5+5;
int n;
int a[maxn];
int dp[maxn];
int cnt;
int main()
{
    int Case=0;
    while(scanf("%d",&n)!=EOF)
    {
        Case++;
        cnt=0;
        for (int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[x]=y;
        }
        for (int i=1;i<=n;i++)
        {
            int loc=lower_bound(dp,dp+cnt,a[i])-dp;
            if(loc==cnt) dp[cnt++]=a[i];
            else dp[loc]=a[i];
        }
        if(cnt==1)
            printf("Case %d:\nMy king, at most %d road can be built.\n\n",Case,cnt);
        else
            printf("Case %d:\nMy king, at most %d roads can be built.\n\n",Case,cnt);
    }
    return 0;
}

2. POJ 3903 Stock Exchange

题目链接:

http://poj.org/problem?id=3903

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+5;
int n;
int cnt;
int a[maxn];
int dp[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        for (int i=1;i<=n;i++)
        {
            int loc=lower_bound(dp,dp+cnt,a[i])-dp;
            if(loc==cnt) dp[cnt++]=a[i];
            else dp[loc]=a[i];
        }
        printf("%d\n",cnt);
    }
    return 0;
}

3. POJ - 1065 Wooden Sticks

题目链接:

https://vjudge.net/problem/POJ-1065

思路:

先排序,然后找最长单调子序列。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=5*1e3+5;
int t;
int n;
int cnt;
struct node
{
    int x,y;
};
node dp[maxn];
node a[maxn];
int vis[maxn];
int no[maxn];
int cur;
int compare (node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y<b.y;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset (vis,0,sizeof(vis));
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        int ans=0;
        sort(a+1,a+n+1,compare);
        int num=n;
        while(num)
        {
            ans++;
            cur=0;
            cnt=0;
            for (int i=1;i<=n;i++)
            {
                if(vis[i]) continue;
                if(cur<=a[i].y)
                {
                    cnt++;
                    vis[i]=1;
                    cur=a[i].y;
                }
            }
            num-=cnt;
        }
        printf("%d\n",ans);
    }
    return 0;
}

4.POJ 1631 Bridging signals

题目链接:

http://poj.org/problem?id=1631

代码如下:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 4*1e4+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int t,n;
int a[maxn];
int L[maxn];
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for (int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        int num=0;
        for (int i=0;i<n;i++)
        {
            int loc=lower_bound(L,L+num,a[i])-L;
            if(loc==num)
            {
                L[num++]=a[i];
            }
            L[loc]=a[i];
        }
        printf("%d\n",num);
    }
	return 0;
}

5. POJ 1952 BUY LOW, BUY LOWER

题目链接:

http://poj.org/problem?id=1952

此题在求最长递减子序列的同时还需要求出方案数。

我们可以通过建立一个cnt数组表示以其为子序列结尾的数目, 这样只要满足dp[j]+1==dp[i]&&a[j]>a[i]就可以为cnt[i]加上cnt[j],

但是如果序列中有相同的数字的话,则在遍历序列的时候将前面相同的数字的cnt 置为0,这样就不会出现重复加的情况了。

代码如下:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 5*1e3+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int n;
int a[maxn];
int L[maxn];
int dp[maxn];
int cnt[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for (int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset (cnt,0,sizeof(cnt));
        int num=0;
        int ans=1;
        for (int i=0;i<n;i++) dp[i]=1;
        for (int i=0;i<n;i++)
        {
            for (int j=i+1;j<n;j++)
            {
                if(a[i]>a[j])
                {
                    dp[j]=max(dp[j],dp[i]+1);
                }
                ans=max(dp[j],ans);
            }
        }
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<i;j++)
            {
                if(a[j]==a[i])
                {
                    cnt[j]=0;
                }
            }
            if(dp[i]==1)
            {
                cnt[i]=1;
            }
            else
            {
                for (int j=0;j<i;j++)
                {
                    if(a[i]<a[j]&&dp[j]+1==dp[i])
                    {
                        cnt[i]+=cnt[j];
                    }
                }
            }
        }
        for (int i=0;i<n;i++)
        {
            if(ans==dp[i])
            {
                num+=cnt[i];
            }
        }
        printf("%d %d\n",ans,num);
    }
	return 0;
}

6. HDU 2881 Jack's struggle

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2881

将两点能否在规定的时间到达进行dp。

代码如下:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int n,m;
struct node
{
    int x,y,t;
};
int compare (node a,node b)
{
    return a.t<b.t;
}
bool judge (node a,node b)
{
    int dis=abs(a.x-b.x)+abs(a.y-b.y);
    if(abs(a.t-b.t)<dis) return false;
    return true;
}
node a[maxn];
int dp[maxn];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for (int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i].t,&a[i].x,&a[i].y);
        }
        int ans=0;
        sort(a,a+m,compare);
        memset (dp,0,sizeof(dp));
        for (int i=0;i<m;i++)
        {
            dp[i]=1;
            for (int j=0;j<i;j++)
            {
                if(judge(a[i],a[j]))
                {
                    dp[i]=max(dp[i],dp[j]+1);
                }
            }
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
	return 0;
}

7.  POJ Crossed Matchings

题目链接:

http://poj.org/problem?id=1692

题解参考:

https://www.cnblogs.com/zjutlitao/p/3572187.html

代码如下:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e2+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int m;
int n1,n2;
int a[maxn],b[maxn];
int dp[maxn][maxn];
int main()
{
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&n1,&n2);
        for (int i=1;i<=n1;i++)
        {
            scanf("%d",&a[i]);
        }
        for (int i=1;i<=n2;i++)
        {
            scanf("%d",&b[i]);
        }
        int ans=0;
        memset (dp,0,sizeof(dp));
        for (int i=1;i<=n1;i++)
        {
            for (int j=1;j<=n2;j++)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(a[i]==b[j]) continue;
                for (int k=1;k<i;k++)
                {
                    for (int l=1;l<j;l++)
                    {
                        if(a[k]==b[j]&&b[l]==a[i])
                        {
                            dp[i][j]=max(dp[i][j],dp[k-1][l-1]+2);
                        }
                    }
                }
                ans=max(ans,dp[i][j]);
            }
        }
        printf("%d\n",ans);
    }
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值