最长上升子序列(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. 与之类似但不同的 ...
随机推荐
- android imageButton 点击按钮前中后,按钮颜色的变化
我们在开发的过程中,往往为了美化界面的需要,会修改按钮的默认外观,而因为Android中的按钮有三种状态—默认,被点击,被选中.所以,如果要改变按钮的外观,需要对这三种情况都做出修改,也许在以往,我们 ...
- 例题.点击按钮显示内容+弹窗效果+ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- pro7
1.本次课学习到的知识点: 函数的作用 确定函数的功能 定义函数 调用函数 2.实验过程中遇到的问题及解决方法: 定义函数时 变量的定义会出现混乱 通过看例题 多练习 逐渐熟悉 需从数学角度解决问题时 ...
- 使用JavaScript创建我的分页
把下面的方法放到一个js文件,页面引用他就行了 JavaScript function PageList(PageSize, PageIndex, TotalCount, ParList) { $(& ...
- C# json object互转工具
public static T Deserializer<T>(string path) { try { System.Xml.XmlDocument xd = new System.Xm ...
- UDF
一:UDF 1.自定义UDF 二:UDAF 2.UDAF 3.介绍AbstractGenericUDAFResolver 4.介绍GenericUDAFEvaluator 5.程序 package o ...
- Sublime text插件使用技巧
1.CSScomb 一个css代码格式化插件,在css文件中或选中css代码,使用快捷键: [ctrl+shift+c],即可实现代码的对齐等格式的优化. mac下修改快捷键: Preferenc ...
- 转:ASP.NET MVC扩展之HtmlHelper辅助方法
1.什么是HtmlHelper辅助方法?其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class F ...
- 面向对象世界里转转七(Liskov替换原则)
前言:Liskov替换原则是关于继承机制的应用原则,是实现开放封闭原则的具体规范,违反了Liskov原则必然意味着违反了开放封闭原则.因此,有必要对面向对象的继承机制及其基本原则做以探索,来进一步了解 ...
- Java学习-024-获取当前类名或方法名二三文
今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 TA 将类名或者方法名直接写死在了代码中...虽说这样可以实现记录类名和方法名,但是当有特殊情况需要修改类名或者方法 ...