(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

Problem Description
JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.

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. ^_^

 
Input
Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.
 
Output
For each test case, output the result in the form of sample. 
You should tell JGShining what's the maximal number of road(s) can be built. 
 
Sample Input
2
1 2
2 1
3
1 2
2 3
3 1
 
Sample Output
Case 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads 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的更多相关文章

  1. 【51NOD-0】1134 最长递增子序列

    [算法]动态规划 [题解]经典模型:最长上升子序列(n log n) #include<cstdio> #include<algorithm> #include<cstr ...

  2. 最长下降子序列O(n^2)及O(n*log(n))解法

    求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...

  3. 最长上升子序列的变形(N*log(N))hdu5256

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. hdu1025 dp(最长上升子序列LIS)

    题意:有一些穷国和一些富国分别排在两条直线上,每个穷国和一个富国之间可以建道路,但是路不能交叉,给出每个穷国和富国的联系,求最多能建多少条路 我一开始在想有点像二分图匹配orz,很快就发现,当我把穷国 ...

  5. Bridging signals---hdu1950(最长上升子序列复杂度n*log(n) )

     题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 一直只知道有除n*n的算法之外的求LIS,但是没学过,也没见过,今天终于学了一下,dp[i]表 ...

  6. 最长上升子序列(LIS)的n*log(n)求法

    方法: 对于某个序列,设一个数组,将序列第一个数放入,然后再一个一个判断序列下一位,如果大于当前数组的末尾元素,则加入数组,否则利用二分法找到第一个大于等于当前数的元素并替换,最后这个数组的长度len ...

  7. O(n log n)求最长上升子序列与最长不下降子序列

    考虑dp(i)表示新上升子序列第i位数值的最小值.由于dp数组是单调的,所以对于每一个数,我们可以二分出它在dp数组中的位置,然后更新就可以了,最终的答案就是dp数组中第一个出现正无穷的位置. 代码非 ...

  8. BZOJ 3173: [Tjoi2013]最长上升子序列

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1524  Solved: 797[Submit][St ...

  9. LCS最长公共子序列(最优线性时间O(n))

    这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...

  10. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

随机推荐

  1. c#循环迭代匿名类链表(可迭代的匿名类)

    Main(){ //为什么?object是基类啊!! //报错.不能从List<anonymous>换成List<object>. //var q=(List<objec ...

  2. mongoose find查询意错点

    //使用mongoose查询文档所有结果时容易出现的问题 ArticleContents.getAll = function(name, callback){ var query = {}; if(n ...

  3. Provisioning Profile

    什么是Provisioning Profile? 从字面翻译,Provisioning Profile就是配置文件的意思,它在开发者账号体系中所扮演的角色也是配置和验证的作用.如果你有开发者账号,可以 ...

  4. AC自动机算法详解

    首先简要介绍一下AC自动机:Aho-Corasick automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章, ...

  5. Android开发笔记:eclipse导入java项目

    若下载的工程文件中含有 .project 则点击菜单栏file-import,选择General-existing project into workplace,点击下一步 选择需要导入的项目文件夹

  6. I Count Two Three---hdu5878(打表+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878 题意:找到第一个>=n的数x, 满足 x = 2a3b5c7d:n<=1e9; 打表 ...

  7. 【转】Android各种Adapter的用法

    转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56)   阅读(143 ...

  8. net异步线程注意事项

      某人(不是我写的)写的异步代码:(还请博友们点评哈~) AsyncEventHandler[] asy = new AsyncEventHandler[ResultCount]; IAsyncRe ...

  9. Interview Check If n Is A Perfect Square

    Check if a given number is a perfect square with only addition or substraction operation. eg. 25 ret ...

  10. js获取时间戳

    1.var now = Date.parse(new Date()); //例:1467342220000 获取的时间戳是把毫秒改成000显示. 2.var now = new Date().valu ...