最长上升子序列(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. 与之类似但不同的 ...
随机推荐
- locality
Computer Systems A Programmer's Perspective Second Edition Well-written computer programs tend to ex ...
- Linguistic corpora 种子语料库-待分析对象-分析与更新语料库
Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...
- var wi = 0; wi < arr.length; wi++
思维 <?php$w = 123;$wb = $w;$w = 456;echo $wb;?><script type="text/javascript"> ...
- html5之canvas初解
<canvas> 元素本身并没有绘制能力(它仅仅是图形的容器) - 必须使用脚本来完成实际的绘图任务. getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属 ...
- backbone-todo案例分析
todo案例可以到这个地址下载 https://github.com/jashkenas/backbone 添加数据后 todo案例不涉及Router,仅有Model.Collection.View的 ...
- HTML知识点总结以及典型例子讲解
一.HTML文本格式化标签(这些标签都不换行) eg: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...
- 一个短小的JS函数,用来得到仅仅包含不重复元素的数组
下面函数主要利用了数组的sort方法,之后的逻辑是看最后一个元素是否等于要添加的元素,如果不是就往尾后加. 这个做法的效率等于sort方法的效率,还过得去. 代码: <!DOCTYPE HTML ...
- JS通用事件监听函数
JS通用事件监听函数 版本一 //把它全部封装到一个对象中 var obj={ readyEvent:function (fn){ if(fn==null){ fn=document; } var o ...
- C语言命名空间
//首先要认识到命名空间的使用条件.#include <stdio.h> typedef struct A { int A; } A; typedef union B { int A; } ...
- 30天,O2O速成攻略【7.18广州站】
活动概况 时间:2015年07月18日13:30-16:30 地点:贝塔咖啡(新港中路TIT创意园内创意西路07号楼) 主办:APICloud.七牛.洪海网络 网址:www.apicloud.com ...