动态规划--HDU--1025

来源:互联网 发布:国际品牌围巾排名知乎 编辑:程序博客网 时间:2024/05/21 00:14


Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16459    Accepted Submission(s): 4689


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
21 22 131 22 33 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.
这道题用于求最长上升子序列。最长上升子序列的问题也是用dp可以很有效率的进行求解。首先,明确一下dp的递归关系。定义dp[i]:以ai为末尾的最长上升子序列的长度。则以ai结尾的上升子序列是:1-->只包含ai的子序列。2-->在满足了j<i&&aj<ai的以aj为上升子列的末尾,追加上ai得到的子序列。则可以得到递推关系为:dp[i]=max{1,dp[j+1]且j<i&&aj<ai;
但是这个递推公式的时间复杂度为O(n^2).下面贴一下这个想法的伪算法。
<pre name="code" class="cpp">/*//数据的输入。int n;//长为n的数列。int a[max];//数列中各个数据。int dp[max];//进行dp数组的定义。int solve(){     int tem=0;    for(int i=1;i<=n;i++)    {               dp[i]=1;        for(int j=1;j<i;j++)        if(a[j[<a[i])        {            dp[i]=max{dp[i],dp[j+1]};        }        tem=max{tem,dp[i]};    }    cout<<tem<<endl;}*/    
下面是真正的题的算法,因为数据量太大,所以不可以用上述算法,要进行二分查找;
<pre name="code" class="cpp">#include <iostream>#include<algorithm>using namespace std;const int size=500050;//用来表示一组数据的两个值。struct wyx{    int x,y;}dyx[size];int dp[size];//表示长度为i的序列的最大递增子序列的最后一个值//运用sort函数时,使用cmp">"代表按照递减的顺序排列,"<"代表按照递增的顺序排列。bool cmp(const wyx &a,const wyx &b){    return a.x<b.x;}int main(){    //求出最大不降子序列的值。    int n;//定义接下来将要输入几组数据。    int p,r;    int count=0;    while(cin>>n)    {        count++;        for(int i=1;i<=n;i++)    {        cin>>dyx[i].x>>dyx[i].y;    }    //排序的时候注意起始位置+1    //按照x的顺序进行递增排列。        sort(dyx+1,dyx+n+1,cmp);        int len=1;//定义的最后输出的最大递增子序列的长度。        int mid;        dp[1]=dyx[1].y;        int s_pos,e_pos;        for(int i=2;i<=n;i++)        {            s_pos=1;            e_pos=len;            //进行二分法,查找需要进行的更新的位置、            //len表示最长递增子序列的长度。            //而dp[i]则表示长度为i的最长递增子序列的最后一个y值。            //要记住每一个更新dp的值的点不一定是连续的。            while(s_pos<=e_pos)            {                 mid=(s_pos+e_pos)/2;                if(dyx[i].y<=dp[mid])                  e_pos=mid-1;                else                  s_pos=mid+1;            }            //二分查找法查找的是一个更新的位置            //上式条件成立代表在dp数组中,取中间的一个变量mid仍旧比当前位置循环的y值大,则选择前一半。            //上式条件不成立代表在dp数组中,中间的变量比当前循环位置的y值小,则选择后一半。            //可以看出上述while循环跳出的条件是,s_pos比len大。            dp[s_pos]=dyx[i].y;            //上述循环跳出后,也可能找到一个已经赋值了的位置,再进行数据的更新、            //只有当将dp中的所有值都判断了一遍之后,上述条件式一直不成立,这是则将其加入最大递增子序列的序列。            if(s_pos>len)            {                len=s_pos;            }        }        //在进行输出的时候,要记得road的单数和复数分开。。。。        if(len==1)        {            cout<<"Case "<<count<<":"<<endl;            cout<<"My king, at most 1 road can be built."<<endl<<endl;        }        else        {            cout<<"Case "<<count<<":"<<endl;            cout<<"My king, at most "<<len<<" roads can be built."<<endl<<endl;        }    }    return 0;}


0 0