codeforces-4D Mysterious Present(二维递增子序列)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GrimCake/article/details/79956465

链接:http://codeforces.com/problemset/problem/4/D

题意:二维递增子序列

题解:先排序,dp【i】表示以i结尾的最长递增子序列的长度,dp【i】= max{dp【j】+1,dp【i】}  j<i, p[i].w>p[j].w&& p[i].h>p[j].h, 注意边界及一些特殊情况

代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

struct node{
    int w, h;
    int id;
}p[5010];

int n;
int w0, h0;
int dp[5010];
int Next[5010];

bool cmp(node a, node b){
    if(a.w == b.w) return a.h<b.h;
    return a.w<b.w;
}

void output(int x){
    if(Next[x] == -1){
        printf("%d", x);
        return;
    }
    output(Next[x]);
    printf(" %d", x);
}
int main(){
    scanf("%d%d%d", &n, &w0, &h0);
    for(int i = 1; i<=n; i++){
        scanf("%d%d", &p[i].w, &p[i].h);
        p[i].id = i;
    }
    sort(p+1, p+1+n, cmp);
    if(p[1].w>w0 && p[1].h>h0) dp[1] = 1;
    memset(Next, -1, sizeof(Next));
    for(int i = 1; i<=n; i++){
        bool flag = true;
        for(int j = 1; j<i; j++){
            //cout<<i<<" "<<j<<" "<<p[i].w<<" "<<p[i].h<<" "<<p[j].w<<" "<<p[j].h<<endl;
            if(p[j].w<p[i].w && p[j].h<p[i].h && p[i].w>w0 && p[i].h>h0){
                //dp[i] = max(dp[i], dp[j]+1);
                //cout<<i<<" "<<dp[i]<<endl;
                if(dp[j]+1>dp[i] && p[j].w>w0 && p[j].h>h0){
                    dp[i] = dp[j]+1;
                    Next[p[i].id] = p[j].id;
                    flag = false;
                }
                //cout<<i<<" "<<dp[i]<<endl;
            }
        }
        if(flag && p[i].w>w0 && p[i].h>h0){
            dp[i] = 1;
        }
    }
    int res = -1;
    int temp;
    for(int i = 1; i<=n; i++){
        //res = max(res, dp[i]);
        if(dp[i]>res){
            res = dp[i];
            temp = p[i].id;
        }
    }
    printf("%d\n", res);
    if(res==0) return 0;
    /*cout<<temp<<endl;
    for(int i = 1; i<=n; i++){
        cout<<Next[i]<<" ";
    }
    cout<<endl;*/
    output(temp);
    printf("\n");



    return 0;
}

/*
5 10 10
22 23
17 19
13 17
8 12
2 6
*/

Mysterious For

10-26

问题描述 :nnMatRush is an ACMer from ZJUT, and he always love to create some special programs. Here we will talk about one of his recent inventions.nThis special program was called "Mysterious For", it was written in C++ language, and contain several simple for-loop instructions as many other programs. As an ACMer, you will often write some for-loop instructions like which is listed below when you are taking an ACM contest.nfor (int i = 0; i < n; i++) n for (int j = i; j < n; j++) n for (int k = j; k < n; k++) n blahblahblah();n n nnnNow, MatRush has designed m for-loop instructions in the "Mysterious For" program, and each for-loop variable was stored in an array a[], whose length is m.nnThe variable i represents a for-loop instructions is the i-th instruction of the "Mysterious For" program.There only two type of for-loop instructions will occur in MatRush’s "Mysterious For" program:nn1-type: if a for-loop belongs to 1-type, it will be an instruction like this:nfor (int a[i] = 0; a[i] < n; a[i]++) n …nnn2-type: if a for-loop belongs to 2-type, it will be an instruction like this:nfor (int a[i] = a[i - 1]; a[i] < n; a[i]++) n …nnnIn addition, after the deepest for-loop instruction there will be a function called HopeYouCanACIt(), here is what’s inside:nvoid HopeYouCanACIt() n puts("Bazinga!");nnnSo, the "Mysterious For" program, obviously, will only print some line of the saying: "Bazinga!", as it designed for.nnFor example, we can assume that n equals to 3, and if the program has three 1-type for-loop instructions, then it will run 33=27 times of the function HopeYouCanACIt(), so you will get 27 "Bazinga!" in total. But if the program has one 1-type for-loop instruction followed by two 2-type for-loop instructions, then it will run 3+2+1+2+1+1=10 times of that function, so there will be 10 "Bazinga!" on the screen.nnNow MatRush has the loop length n and m loop instructions with certain type, then he want to know how many "Bazinga!" will appear on the screen, can you help him? The answer is too big sometimes, so you just only to tell him the answer mod his QQ number:364875103.nnAll for-loop instructions are surely nested. Besides, MatRush guaranteed that the first one belongs to the 1-type. That is to say, you can make sure that this program is always valid and finite. There are at most 15 1-type for-loop instructions in each program.nn输入:nnFirst, there is an integer T(T<=50), the number of test cases.nFor every case, there are 2 lines.nThe first line is two integer n(1<=n<=1000000) and m(1<=m<=100000) as described above.nThe second line first comes an integer k(1<=k<=15), represents the number of 1-type loop instructions, then follows k distinctive numbers, each number is the i-th 1-type loop instruction’s index(started from 0), you can assume the first one of this k numbers is 0 and all numbers are ascending.nAll none 1-type loop instructions of these m one belongs to 2-type.n输出:nnFirst, there is an integer T(T<=50), the number of test cases.nFor every case, there are 2 lines.nThe first line is two integer n(1<=n<=1000000) and m(1<=m<=100000) as described above.nThe second line first comes an integer k(1<=k<=15), represents the number of 1-type loop instructions, then follows k distinctive numbers, each number is the i-th 1-type loop instruction’s index(started from 0), you can assume the first one of this k numbers is 0 and all numbers are ascending.nAll none 1-type loop instructions of these m one belongs to 2-type.n样例输入:nn5n3 3n3 0 1 2n3 3n1 0n3 3n2 0 2n4 4n4 0 1 2 3n10 10n10 0 1 2 3 4 5 6 7 8 9n样例输出:nnCase #1: 27nCase #2: 10nCase #3: 18nCase #4: 256nCase #5: 148372219

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试