CF 4D Mysterious Present

原创 2012年03月30日 19:14:02

dfs题!!用Dp!可是就这么简单的思路,我偏偏绕远路,用LCS来DP求,被无情的MLE!

#include<iostream>
#include<cstring>
using namespace std;
int dfs(int);
int w[5001],h[5001],d[5001],p[5001],n;
main()
{
	cin>>n;
	for(int i=0;i<=n;i++)
	cin>>w[i]>>h[i];
	memset(d,0,sizeof(d));
	int k=dfs(0);
	cout<<k<<"\n";
	int i=0;
	while(p[i])
	{
		cout<<p[i]<<" ";
		i=p[i];
	}



	}
int dfs(int x)
{
	
	if(d[x])return d[x];
	for(int i=0;i<=n;i++)
	{
		if(w[i]>w[x] && h[i]>h[x])
		{
			int t=dfs(i);
			if(d[x]<t+1)
			{
				d[x]=t+1;
				p[x]=i;
			}
		}
	}
	return d[x];
}			

我的MLE代码~

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Pair
{
    int v,id;
}w[5010],h[5010];
bool cmp(Pair a,Pair b)
{
    return a.v < b.v;
}
int  dp[5010][5010],ww[5010],hh[5010],r[5010][5010],cot[5010],k = 0;

bool a[1000010];
void print(int i,int j)
{
    if(i == 0||j == 0) return;
    if(r[i][j] == 3) {print(i-1,j-1);cot[k++] = ww[i];}
    else if(r[i][j] == 2) print(i-1,j);
    else print(i,j-1);
}
int main()
{
    //freopen("a.txt","r",stdin);
    int n,i,ans = 0,ii,jj,j,k1,k2;
    cin>>n>>w[0].v>>h[0].v;
    //int s[5010];
    memset(dp,0,sizeof(dp));
    w[0].id = 0;h[0].id = 0;
    for(i = 1;i <= n;i ++)
    {
        cin>>w[i].v >>h[i].v;
        w[i].id = h[i].id = i;
        //cout<<"dsa"<<endl;
    }
    sort(w,w + n+1,cmp);
    sort(h,h + n+1,cmp);
    k1 = 0;k2 = 0;
    memset(a,0,sizeof(a));
    //cout<<"dsa"<<endl;
    for(i = 0;i <= n;i ++)
    if(w[i].id == 0) break;
    while(i <= n) {if(a[w[i].v]==0){ww[k1++] = w[i].id;a[w[i].v] = 1;}i ++;}
    //cout<<k1<<k2<<endl;
    memset(a,0,sizeof(a));
    for(i = 0;i <= n;i ++)
    if(h[i].id == 0) break;
    while(i <= n) {if(a[h[i].v]==0){hh[k2++] = h[i].id;a[h[i].v] = 1;}i ++;}
    //cout<<k1<<k2<<endl;
    for(i = 0;i < 5010;i ++) {dp[i][0] = 0;dp[0][i] = 0;}
    for(i = 1;i < k1;i ++)
    {
        for(j = 1;j < k2;j ++)
        {
            if(ww[i] == hh[j]) {dp[i][j] = dp[i-1][j-1]+1;r[i][j] = 3;}
            else if(dp[i-1][j] >= dp[i][j-1]) {dp[i][j] = dp[i-1][j];r[i][j] = 2;}
            else {dp[i][j] = dp[i][j-1];r[i][j] = 1;}
        }
    }
    //cout<<k1<<k2<<endl;
    cout<<dp[k1-1][k2-1]<<endl;
    print(k1-1,k2-1);
    if(k > 0) cout<<cot[0];
    for(i = 1;i < k;i ++) cout<<" "<<cot[i];
}


相关文章推荐

Codeforces Beta Round #4 (Div. 2 Only)D. Mysterious Present

D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes ...
  • tjqACM
  • tjqACM
  • 2014年08月25日 20:34
  • 266

Codeforces Beta Round #4 (Div. 2 Only)——D. Mysterious Present

题意: 现在我们有n封信封,然后我们有一张卡片,并且我们知道它的长度和宽度。 然后n行我们知道每个信封的长度和宽度。 现在我们想形成一个链,这条链的长度就是这条链中所含有的信封的数量,但是必须同时保证...

Codeforce 4D - Mysterious Present

原题: D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabyte...

Codeforces 4D Mysterious Present 求数对的最长上升序列 dp

D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes ...

codeforces 4D Mysterious Present -- 动态规划

D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes ...

codeforces 4D. Mysterious Present

D. Mysterious Present time limit per test 1 second memory limit per test 64 megabytes ...
  • steveyg
  • steveyg
  • 2015年03月04日 12:35
  • 290

Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present

最长上升子序列,这种水题还是一眼就能看出来的。 题目大意: 主人公想在一张w*h的明信片外套信封。他有n个信封,每个信封的长宽给出,问最多能套多少层。给出从小到大的顺序。 ...

Codeforces 4D. Mysterious Present

D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes input s...

Codeforces 4D Mysterious Present

EnglishDescPeter decided to wish happy birthday to his friend from Australia and send him a card. To...

codeforce 4D Mysterious Present 简单思路 动态规划

看网上的代码和思路,感觉好复杂的,决定自己写一个代码简洁,思路简单的代码。 这题的意思就是要找出一个最大子序列,这个最大子序列的每个元素就是一个信封,每个信封包含两部分,即信封的宽度和长度,要求序列中...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CF 4D Mysterious Present
举报原因:
原因补充:

(最多只允许输入30个字)