Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present
来源:互联网 发布:多多软件站解压密码 编辑:程序博客网 时间:2024/05/17 03:18
最长上升子序列,这种水题还是一眼就能看出来的。
题目大意:
主人公想在一张w*h的明信片外套信封。他有n个信封,每个信封的长宽给出,问最多能套多少层。给出从小到大的顺序。
解题思路:
最长上升子序列,只不过是记忆路径。
下面是代码:
#include <set>#include <map>#include <queue>#include <math.h>#include <vector>#include <string>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <cctype>#include <algorithm>#define eps 1e-10#define pi acos(-1.0)#define inf 107374182#define inf64 1152921504606846976#define lc l,m,tr<<1#define rc m + 1,r,tr<<1|1#define zero(a) fabs(a)<eps#define iabs(x) ((x) > 0 ? (x) : -(x))#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))#define clearall(A, X) memset(A, X, sizeof(A))#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))#define memcopyall(A, X) memcpy(A , X ,sizeof(X))#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )#define min( x, y ) ( ((x) < (y)) ? (x) : (y) )using namespace std;struct node{ int w,h,num; bool operator <(const node a)const { if(w+h==a.w+a.h) { if(w==a.w)return h<a.h; else return w<a.w; } return w+h<a.w+a.h; }} envelopes[5000];int cnt;int dp[5005],pre[5005];void output(int num){ if(pre[num]!=-1)output(pre[num]); printf("%d ",envelopes[num].num); return ;}int main(){ int n,w,h; cnt=0; scanf("%d%d%d",&n,&w,&h); for(int i=0; i<n; i++) { scanf("%d%d",&envelopes[cnt].w,&envelopes[cnt].h); envelopes[cnt].num=i+1; if(envelopes[cnt].w>w&&envelopes[cnt].h>h)cnt++; } if(cnt==0) { puts("0"); return 0; } clearall(pre,-1); sort(envelopes,envelopes+cnt); int maxnum=1,maxp=0; dp[0]=1; for(int i=1; i<cnt; i++) { int max1=-1,mp=-1; for(int j=i-1; j>=0; j--) { if(envelopes[j].w<envelopes[i].w&&envelopes[j].h<envelopes[i].h&&max1<dp[j]) { max1=dp[j]; mp=j; } } dp[i]=max1+1; pre[i]=mp; if(dp[i]>maxnum) { maxnum=dp[i]; maxp=i; } } printf("%d\n",maxnum); output(maxp); return 0;}
0 0
- Codeforces Beta Round #4 (Div. 2 Only)D. Mysterious Present
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present
- Codeforces Beta Round #4 (Div. 2 Only)——D. Mysterious Present
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present 记忆化搜索
- Mysterious Present Codeforces Beta Round #4 (Div. 2 )problem D
- Codeforces Beta Round #4 (Div. 2 Only)
- Codeforces Beta Round #4 (Div. 2 Only)
- Codeforces Beta Round #4 (Div. 2 Only)
- Codeforces Beta Round #4 (Div. 2 Only)
- 【KMP】Codeforces Beta Round #93 (Div. 2 Only) D
- Codeforces Beta Round #75 (Div. 2 Only)——D
- Codeforces Beta Round #18 (Div. 2 Only) D - Seller Bob
- Codeforces Beta Round #91 (Div. 2 Only) D. Lucky Transformation
- Codeforces Beta Round #22 (Div. 2 Only)-D. Segments
- codeforces 4D. Mysterious Present
- codeforces 4D Mysterious Present
- Codeforces 4D. Mysterious Present
- codeforces 4D. Mysterious Present
- Sonatype Nexus中央仓库索引下载失败解决方法
- jquery设置背景图片
- 算法导论---学习笔记012
- LayoutInflater的获取与使用
- HTTP请求报文和HTTP响应报文
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present
- JQuery 正则表达式
- What happens when you type a URL in browser
- 设计模式之组合模式
- mini6410 中断之tasklet和工作队列
- swift 视频教程
- 卖饼人生
- 今天写下文字,明天造就传奇。
- 关于51单片机的一些基本知识