NYOJ236 心急的C小加&NYOJ 16 矩形嵌套
2014-04-17 19:45
316 查看
分析题目,一求单调递增子序列个数,一求最长单调子序列。
故而,解题方法不一,贪心PK动态规划
NYOJ 236 心急的C小加 贪心
NYOJ 16 矩形嵌套 DP
故而,解题方法不一,贪心PK动态规划
NYOJ 236 心急的C小加 贪心
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct mb { int len; int weight; }w[10001]; bool cmp(mb x,mb y) //排序 { if(x.len<y.len) return true; //升序 if(x.len==y.len&&x.weight<y.weight) return true; return false; } int main() { int s,n,i,j,count,t; scanf("%d",&s); while(s--) { count=0; scanf("%d",&n); for(i=0;i<=n-1;i++) { scanf("%d %d",&w[i].len,&w[i].weight); } sort(w,w+n,cmp); for(i=0;i<=n-1;i++) { if(w[i].weight!=0) //数未出现过 { t=w[i].weight; count++; //开启一单位 for(j=i+1;j<=n-1;j++) { if(w[j].weight>=t) { t=w[j].weight; w[j].weight=0; } } } } printf("%d\n",count); } return 0; }
NYOJ 16 矩形嵌套 DP
#include<cstdio> #include<algorithm> #include<cstring> #define M 1024 using namespace std; struct rect{ int len,width; }d[M],p[M]; bool cmp(rect a,rect b)//按长排降序,后再按宽排 { if(a.len==b.len) return a.width<b.width; return a.len>b.len; } int main(){ int m; scanf("%d",&m); while(m--) { int n,i,j,t; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d %d",&d[i].len,&d[i].width); if(d[i].width>d[i].len) { t=d[i].width; d[i].width=d[i].len; d[i].len=t; } } sort(d,d+n,cmp); int length=1; p[0].len=900,p[0].width=900; //单调递减最长子序列 for(i=0;i<n;i++)//终止位置 { for(j=length-1;j>=0;j--)//length 长度 { if(d[i].width<p[j].width&&d[i].len<p[j].len) { p[j+1]=d[i]; if(j+1==length) length++; break;} } } printf("%d\n",length-1); } }
相关文章推荐
- nyoj 16 矩形嵌套 & nyoj 17 单调递增最长子序列
- nyoj 16 矩形嵌套&&第七届河南省acm省赛H题
- nyoj 心急的C小加 236 (贪心&&LIS)
- nyoj--236--心急的C小加(动态规划&&LIS)
- nyoj--236--心急的C小加(动态规划&&LIS)
- 木棒 xynuoj 1737 && 心急的C小加 nyoj 236
- NYOJ 16 矩形嵌套(动态规划)
- nyoj 16 矩形嵌套
- NYOJ - 16 矩形嵌套
- 【ACM菜逼解题报告】矩形嵌套(NYOJ16)
- NYOJ 16 矩形嵌套
- NYOJ 16 矩形嵌套
- NYOJ 236 心急的C小加
- NYOJ 236 心急的C小加
- NYOJ-16-矩形嵌套
- nyoj 16 矩形嵌套
- nyoj 236 心急的c小加 【LIS】
- NYOJ-16 矩形嵌套
- nyoj 236 心急的C小加
- nyoj 16 矩形嵌套