hud 1025 Constructing Roads In JGShining's Kingdom
来源:互联网 发布:linux virtualbox装osx 编辑:程序博客网 时间:2024/05/19 07:08
题目
http://acm.hdu.edu.cn/showproblem.php?pid=1025分析
本题要求求出互不交叉的能够相容的最多的道路,想到这里觉得必须对所有的道路按照一定的规则进行排序(此处为经验,多练习即可)。在题目给出的n条道路有序之后,但询问与第j条道路相容的所以道路的时候,根据动态规划的最有子结构和重叠子问题性质,只用考虑前边j-1个与j的相容情况。
因此,为了求出与j的最大相容道路,只用再次遍历前j-1,找出最大的与j相容的道路个数即可。
复杂度分析
两重循环,时间复杂度为O(n*n),空间复杂度为O(n)
代码
#include <cstdio>#include <vector>#include <algorithm>using namespace std;typedef struct point1025{point1025(int a,int b){ x=a;y=b;v=1; }int x,y,v;} Point1025;bool sortcir(Point1025 X,Point1025 Y){if(X.x>Y.x||X.x==Y.x&&X.y>Y.y) return true;return false;}bool operator>(const Point1025 &X,const Point1025 &Y){return (X.x>Y.x && X.y>Y.y);}int main(){freopen("in.txt","r",stdin);vector<Point1025> coll1025;int n1025,p1025,r1025;int k=0,max;while(scanf("%d",&n1025)){coll1025.clear();max=0;while(n1025--){scanf("%d%d",&p1025,&r1025);coll1025.push_back(Point1025(p1025,r1025));}sort(coll1025.begin(),coll1025.end(),sortcir);for(int i=0;i<coll1025.size();++i){for(int j=0;j<i;++j){if(coll1025[j] > coll1025[i]){coll1025[i].v=(coll1025[i].v>coll1025[j].v+1)?coll1025[i].v:(coll1025[j].v+1);}}max=max>coll1025[i].v?max:coll1025[i].v;}printf("Case %d:\nMy king, at most %d road can be built.\n",++k,max);}return 0;}
- hud 1025 Constructing Roads In JGShining's Kingdom
- Constructing Roads In JGShining's Kingdom
- HDUOJ1025 Constructing Roads In JGShining's Kingdom
- Constructing Roads In JGShining's Kingdom
- hdu1025 Constructing Roads In JGShining's Kingdom
- Constructing Roads In JGShining's Kingdom
- Constructing Roads In JGShining's Kingdom
- HDU1025 Constructing Roads In JGShining's Kingdom
- hdu1025-Constructing Roads In JGShining's Kingdom
- HDU1025 Constructing Roads In JGShining's Kingdom
- HDU1025 Constructing Roads In JGShining's Kingdom
- hdu1025 Constructing Roads In JGShining's Kingdom
- hdu Constructing Roads In JGShining's Kingdom
- HDU1025 Constructing Roads In JGShining's Kingdom
- hdu1025 Constructing Roads In JGShining's Kingdom
- HDU1025 Constructing Roads In JGShining's Kingdom
- hdu1025 Constructing Roads In JGShining's Kingdom
- Constructing Roads In JGShining's Kingdom
- C/C++中的枚举类型
- Linux实验二:Linux 内核模块测试
- 用C++实现数据结构二 带头结点的单链表
- C/C++内存问题检查利器—Purify (四)
- HDU2137:circumgyrate the string
- hud 1025 Constructing Roads In JGShining's Kingdom
- 整形数按位逆置
- dispatch_once 优化代码性能
- 美能达老镜头选购参考
- 关于Android x86的启动参数设置相关探讨
- servlet多线程
- HDU 3068 最长回文 / POJ 3974 Palindrome
- C/C++内存问题检查利器—Purify (五)
- JavaScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException: 未能分析从服务器收到的消息。