codeforces4D - Mysterious Present DP

题意:求限定起始位置的二维最长递增子序列.

解题思路:直接DP

解题代码:

  1 // File Name: 4d.cpp
  2 // Author: darkdream
  3 // Created Time: 2014年08月04日 星期一 19时24分49秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25
 26 using namespace std;
 27
 28 struct node{
 29   int from;
 30   int len;
 31 }dp[5000];
 32 struct node1{
 33   int w, h,s;
 34 }a[5005];
 35 int ok[10000];
 36 int cmp(node1 a,node1 b)
 37 {
 38    if(a.w == b.w)
 39        return a.h < b.h;
 40    return a.w < b.w;
 41 }
 42 void dfs(int k)
 43 {
 44   if(k == -1)
 45       return;
 46    dfs(dp[k].from);
 47    if(a[k].s)
 48    printf("%d ",a[k].s);
 49
 50 }
 51 int main(){
 52    int n ;
 53    scanf("%d %d %d",&n,&a[0].w,&a[0].h);
 54    memset(ok,0,sizeof(ok));
 55    for(int i =1 ;i <= n;i ++)
 56    {
 57        scanf("%d %d",&a[i].w,&a[i].h);
 58        a[i].s = i;
 59    }
 60    sort(a+1,a+1+n,cmp);
 61  /*  for(int i = 1;i <= n;i ++)
 62    {
 63        printf("%d %d\n",a[i].w,a[i].h);
 64    }*/
 65    for(int i = 0 ;i <= n;i ++)
 66    {
 67       dp[i].from = i ;
 68       dp[i].len = 1;
 69    }
 70    dp[0].from = -1;
 71    ok[0] = 1;
 72    int asite = 0;
 73    int amx = 1;
 74    for(int i = 1;i <= n;i ++)
 75    {
 76       int mx = 0;
 77       int site = -1 ;
 78       for(int j = 0;j < i;j ++)
 79       {
 80         if(a[i].w > a[j].w && a[i].h > a[j].h && ok[j])
 81         {
 82             ok[i] = 1;
 83             if(dp[j].len > mx)
 84             {
 85                mx = dp[j].len;
 86                site = j;
 87             }
 88         }
 89       }
 90       dp[i].len = mx +1;
 91       dp[i].from = site;
 92       if(dp[i].len > amx)
 93       {
 94         amx = dp[i].len ;
 95         asite = i;
 96       }
 97       //printf("%d\n",dp[i].len);
 98    }
 99    printf("%d\n",amx-1);
100    dfs(asite);
101 return 0;
102 }

codeforces4D - Mysterious Present DP,布布扣,bubuko.com

时间: 2024-08-05 19:57:46

codeforces4D - Mysterious Present DP的相关文章

codeforces 4D D. Mysterious Present(dp)

题目连接: codeforces 4D 题目大意: 给出n个信封,这n个信封有长和宽,给出卡片的尺寸,求取能够装入卡片的最长的序列,序列满足后一个的长和宽一定大于前一个,求最长的这个序列的长度,并且给出一组可行解. 题目分析: 一看这种题目就是dp的题目,状态定义dp[i]为以i结尾的序列的最大的长度,并且利用一个数组记录得到最优解的路径,采取链表的形式进行存储. 首先对给出的信封进行排序,按照宽为第一关键字,高为第二关键字, 状态转移方程如下: dp[i]=maxj=0j?1{dp[j]+1}

HDU - 2145 zz&#39;s Mysterious Present (最短路)

题目大意:有一个人,办了一个party,先到的有礼物.如果有多个人同时到达,就去家离得最远的那个,如果还是多个人同时到达,就去那个编号最大的 解题思路:最短路水题... #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> using namespace std; #define N 310 #define INF 0

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

最长上升子序列,这种水题还是一眼就能看出来的. 题目大意: 主人公想在一张w*h的明信片外套信封.他有n个信封,每个信封的长宽给出,问最多能套多少层.给出从小到大的顺序. 解题思路: 最长上升子序列,只不过是记忆路径. 下面是代码: #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #

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

传送门 题意: 现在我们有 n 个信封,然后我们有一张卡片,并且我们知道这张卡片的长和宽. 现给出这 n 个信封的长和宽,我们想形成一个链,这条链的长度就是这条链中所含有的信封的数量: 但是需要满足①信封a可以连接信封b当且仅当信封a的长和宽分别严格小于信封b的长和宽.       ②构成这条长链的所有信封的长和宽分别严格小于卡片的长和宽. 问最多可以形成多长的链,并且输出我们选取的链的编号: 题解: DAG上的动态规划: 如果信封对于任意两个信封 a,b 满足上述条件①②,那么连一条由a指向b

2017-4-16-Train:Codeforces Beta Round #4 (Div. 2 Only)

反思: 我去确实很菜啊,写不动数据结构,写不动trick题,脑子转不动. 可能一直在写专题,都是不需要思考用什么方法的题目,所以都没有经过太多的思考.就像今天的DIV2.实话真的简单,但是我只有一题1A,其中B题没读到题目细节,还有D题没想到DP去做,而是直接写模拟了.唉,说多了都是泪,加油训练吧 A. Watermelon(水题) One hot summer day Pete and his friend Billy decided to buy a watermelon. They cho

CUGBACM Codeforces Tranning 2 题解

链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=62027#overview 描述:现场过了四道,最后一题模拟退火. 题解: A.Chat Server's Outgoing Traffic 题意:一个聊天室,每次操作有三种选择,第一种是有人进入,第二种有人出去,第三种有人发言,每次发言产生一定影响,影响为人数*字数,问总价值多少. 思路:水,模拟即可. 代码: #include <algorithm> #include <

【转载】夜深人静写算法(四)——差分约束

[转载]夜深人静写算法(四) - 差分约束  目录     一.引例       1.一类不等式组的解   二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列       5.Bellman-Ford       6.SPFA       7.Floyd-Warshall   三.差分约束        1.数形结合        2.三角不等式        3.解的存在性        4.最大值

cf 1163D Mysterious Code (字符串, dp)

大意: 给定字符串$C$, 只含小写字母和'*', '*'表示可以替换为任意小写字母, 再给定字符串$S,T$, 求$S$在$C$中出现次数-$T$在$C$中出现次数最大值. 设$dp[i][j][k]$表示$C$的前$i$位, $S$和$T$分别匹配到第$j$位和第$k$位的最优解 可以用$kmp$优化转移, 复杂度是$O(26^2m^2n)$, 优化一下$kmp$的匹配的话可以达到$O(26m^2n)$ #include <iostream> #include <sstream>

HDU1520——树形DP——Anniversary party

Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E.