最长上升子序列(N*log(N))hdu1025
(HDU1025)
Constructing Roads In JGShining's Kingdom
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18804 Accepted Submission(s): 5311
Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.
With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.
Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II.
The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones.
But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.
For example, the roads in Figure I are forbidden.

In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
You should tell JGShining what's the maximal number of road(s) can be built.
题意:上面n个点,下面n个点,然后在这2n个点之间随意连线,一个点只能被连一次,问最多有多少条线不交叉。
方法一:upper_bound()容器
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
int a[M],b[M],n;
int finde()
{
int t=;
b[t]=a[];
t++;
for(int i=;i<=n;i++)
{
int id=upper_bound(b,b+t,a[i])-b;
//在b数组中弹出比ai大的最左边的元素,然后返回下标,否则返回last的下标
if(id==t)
t++;
b[id]=a[i];
}
return t;
}
int main()
{
int kk=;
while(scanf("%d",&n)!=-)
{ for(int i=;i<=n;i++)
{
int k,p;
scanf("%d%d",&k,&p);
a[k]=p;
}
int leng=finde();
printf("Case %d:\n",kk++);
if(leng==)
cout<<"My king, at most "<< leng <<" road can be built."<<endl;
else
cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
cout<<endl;
}
return ;
}
方法二:二分查找
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
int a[M],b[M],c[M],n;
int finde(int n,int k)
{
int l=;
int r=n;
while(l<=r)
{
int mid=(l+r)/;
if(c[mid]<k)
l=mid+;
else
r=mid-;
}
return l;
}
int main()
{
int kk=;
while(scanf("%d",&n)!=-)
{ for(int i=;i<=n;i++)
{
int k,p;
scanf("%d%d",&k,&p);
a[k]=p;
}
memset(c,inf,sizeof(c));
b[]=;
c[]=a[];
for(int i=;i<=n;i++)
{
int id=finde(n,a[i]);
c[id]=a[i];
b[i]=id;
}
int leng=;
for(int i=;i<=n;i++)
leng=max(leng,b[i]); printf("Case %d:\n",kk++);
if(leng==)
cout<<"My king, at most "<< leng <<" road can be built."<<endl;
else
cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
cout<<endl;
}
return ;
}
方法三:模拟upper_bound
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 100009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
int a[M],b[M],c[M],n;
int binary_find(int n,int k)
{
int l=;
int r=n-;
while(l<=r)
{
int mid=(l+r)/;
if(b[mid]>=k)
r=mid-;
else
l=mid+;
}
return l;
}
int fun(int n)
{
int t=;
b[t]=a[];
t++;
for(int i=;i<=n;i++)
{
int id=binary_find(t,a[i]);
if(id==t)
t++;
b[id]=a[i];
}
return t;
}
int main()
{
int kk=;
while(scanf("%d",&n)!=-)
{ for(int i=;i<=n;i++)
{
int k,p;
scanf("%d%d",&k,&p);
a[k]=p;
}
int leng=fun(n);
printf("Case %d:\n",kk++);
if(leng==)
cout<<"My king, at most "<< leng <<" road can be built."<<endl;
else
cout<<"My king, at most "<< leng <<" roads can be built."<<endl;
cout<<endl;
}
return ;
}
最长上升子序列(N*log(N))hdu1025的更多相关文章
- 【51NOD-0】1134 最长递增子序列
[算法]动态规划 [题解]经典模型:最长上升子序列(n log n) #include<cstdio> #include<algorithm> #include<cstr ...
- 最长下降子序列O(n^2)及O(n*log(n))解法
求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...
- 最长上升子序列的变形(N*log(N))hdu5256
序列变换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu1025 dp(最长上升子序列LIS)
题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...
- Bridging signals---hdu1950(最长上升子序列复杂度n*log(n) )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 一直只知道有除n*n的算法之外的求LIS,但是没学过,也没见过,今天终于学了一下,dp[i]表 ...
- 最长上升子序列(LIS)的n*log(n)求法
方法: 对于某个序列,设一个数组,将序列第一个数放入,然后再一个一个判断序列下一位,如果大于当前数组的末尾元素,则加入数组,否则利用二分法找到第一个大于等于当前数的元素并替换,最后这个数组的长度len ...
- O(n log n)求最长上升子序列与最长不下降子序列
考虑dp(i)表示新上升子序列第i位数值的最小值.由于dp数组是单调的,所以对于每一个数,我们可以二分出它在dp数组中的位置,然后更新就可以了,最终的答案就是dp数组中第一个出现正无穷的位置. 代码非 ...
- BZOJ 3173: [Tjoi2013]最长上升子序列
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1524 Solved: 797[Submit][St ...
- LCS最长公共子序列(最优线性时间O(n))
这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
随机推荐
- java CountDownLatch
Listing -. Using a Countdown Latch to Trigger a Coordinated Start import java.util.concurrent.CountD ...
- java开发bug 在启动Tomcat 6.0时发现第一条信息便是
MyEclipse 8.5 + tomcat6 + jdk 1.8 启动的时候报错: The APR based Apache Tomcat Native library which allows o ...
- Bootstrap 进度条媒体对象和 Well 组件
一.Well 组件 这个组件可以实现简单的嵌入效果. //嵌入效果 <div class="well"> Bootstrap </div> //有 lg 和 ...
- nrf51822裸机教程-GPIOTE
GPIO通常都会具有中断功能,上一讲的GPIO中并没有涉及到中断的相关寄存器. 51822将GPIO的中断相关做成了一个单独的模块GPIOTE,这个模块不仅提供了GPIO的中断功能,同时提供了 通过t ...
- TOMCAT源码分析(启动框架)
建议: 毕竟TOMCAT的框架还是比较复杂的, 单是从文字上理解, 是不那么容易掌握TOMCAT的框架的. 所以得实践.实践.再实践. 建议下载一份TOMCAT的源码, 调试通过, 然后单步跟踪其启动 ...
- hive运行的相关配置
一:执行SQL的方式 1.配置的键值 2.minimal下运行fetch 3.设定hive.fetch.task.conversion=more 4.在more下运行fetch 二:虚拟列 一共三个虚 ...
- Qt Focus事件,FocusInEvent()与FocusOutEvent()
描述:一开始我要实现的目的就是,在一个窗体上有多个可编辑控件(比如QLineEdit.QTextEdit等),当哪个控件获得焦点,哪个控件的背景就高亮用来起提示作用,查了下文档应该用focusInEv ...
- 设计模式:职责链模式(Chain Of Responsibility)
定 义:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. 结构图: 处理请求类: //抽象处理类 abs ...
- JSON格式解析和libjson使用简介(关于cjson的使用示例)
JSON格式解析和libjson使用简介 在阅读本文之前,请先阅读下<Rss Reader实例开发之系统设计>一文. Rss Reader实例开发中,进行网络数据交换时主要使用到了两种数据 ...
- [LeetCode] Combinations (bfs bad、dfs 递归 accept)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...