HDU_1025_Constructing Roads In JGShining's Kingdom


                         Constructing Roads In JGShining's Kingdom

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


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.
Hint
Huge input, scanf is recommended.


题意还是很好理解的,这里不再赘述,注意输入未必是按顺序的以及只有一条路和多条路时输出的road单复数,编号从1到n。最先想到的是用dp求最长不增子序列,交上去果断T,因为数据范围太大,dp复杂度n^2,明显会超时,这里任然附上dp代码仅供参考思路。

下述栈均为模拟栈数组simulate_stack

这里用到二分加上模拟栈的方式来处理,复杂度n*logn,其中对模拟战数组simulate_stack的处理过程中用到了贪心策略,先将poor city 1进栈,再对后面的poor city依次遍历如果遍历到的该位元素即大于栈顶元素就使之进栈,反之(这里只有大于和小于的情况,因为题中明确了不会有不同的poor city 连接到同一个rich city)对栈中已有元素采用二分查找,找到栈中第一个大于该元素的位置并与之替换,注意,此时栈中元素即序列长度并没有增加,但是,其可增加的“潜力”增大了,这就是本题采用的贪心策略。

举例如下:

 序列中元素:1 3 8 2 5 7 9

栈中元素:1

                  1 3

                  1 3 8

                  1 2 8                          //  3 与 2 替换                   

                  1 2 5                          //  8 与 5 替换

                  1 2 5 7 9

例子可能举得不太好,1 2 5 7 9 和 1 3 5 7 9都是最长的子序列,题中只需要求序列长度, 明白这种处理方法的意思即可!


值得注意的是:每次更新模拟栈数组时,如果是将数字放在模拟栈数组最后面(栈顶)则能保证子序列的正确性,如果是在中间插入,未必能保证子序列(模拟栈数组中的元素的排列顺序 与原序列对比)的正确,但是到目前为止,子序列的最长长度一定是模拟栈数组中元素的个数。



/***********************************

          二分+贪心
              复杂度 n*logn 
欢迎交流,还请各位不吝赐教!
                  
***********************************/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>

#define PI acos(-1.0)
#define eps 1e-10
#define INF 0x7fffffff
#define MOD 1000000007
#define debug(x) cout << "--------------> " << x << endl

typedef __int64 ll;
typedef long long LL;
typedef unsigned long long ULL;

using namespace std;

const int MAXN = 500088;

int simulate_stack[MAXN];                               //模拟栈数组
int poor_rich[MAXN];                                    //连接,下标为poor city编号,存的是与之连接的rich city编号
int len;                                                

int find_pos(int num, int l, int r)                     //二分查找
{
        int mid;
        while(l <= r)
        {
                mid = (l+r) >> 1;
                if(simulate_stack[mid] < num)
                {
                        l = mid+1;
                }
                else    r = mid-1;
        }
        return l;
}

int main()
{
        int n, index, tmp, len;
        int cnt = 0;
        while(~scanf("%d", &n))
        {
                cnt++;
                len = 1;                         
                memset(poor_rich, 0, sizeof(poor_rich));
                memset(simulate_stack, 0, sizeof(simulate_stack));

                for(int i = 0; i < n; ++i)
                {
                        scanf("%d%d", &index, &tmp);
                        poor_rich[index] = tmp;
                }
                simulate_stack[1] = poor_rich[1];      //先将poor_city1入栈
                for(int i = 2; i <= n; ++i)            
                {
                        if(simulate_stack[len] < poor_rich[i])                //大于栈顶元素,直接入栈,长度len++
                        {
                                len++;
                                simulate_stack[len] = poor_rich[i];
                        }
                        else
                        {
                                int pos = find_pos(poor_rich[i], 0, len);     //小于栈顶元素,找栈中第一个大于该元素的元素并替换
                                simulate_stack[pos] = poor_rich[i];
                        }
                }
                if(len > 1)
                printf("Case %d:\nMy king, at most %d roads can be built.\n\n", cnt, len);
                else
                printf("Case %d:\nMy king, at most %d road can be built.\n\n", cnt, len);
        }
}





/**附上dp解法,虽然会T,仅供参考**/

/*************************************************
                   dp法
                              复杂度n^2
*************************************************/

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>

#define PI acos(-1.0)
#define eps 1e-10
#define INF 0x7fffffff
#define MOD 1000000007
#define debug(x) cout << "--------------> " << x << endl

typedef __int64 ll;
typedef long long LL;
typedef unsigned long long ULL;

using namespace std;

int dp[500088];                           //dp[i]表示从i开始修路最多可修路的总数
int poor_rich[500088];

int main()
{
     int n, index;
     while(~scanf("%d", &n))
     {
        index = 0;
        memset(dp, -1, sizeof(dp));
        memset(poor_rich, 0, sizeof(poor_rich));
        for(int i = 0; i < n; ++i)
        {
                scanf("%d%d", &index, &poor_rich[index++]);
        }
        for(int i = 0; i < n; ++i)
        {
                dp[i] = 1;
                for(int j = 0; j < i; ++j)
                {
                        if(poor_rich[j] < poor_rich[i] && dp[j]+1 > dp[i])
                                dp[i] = dp[j]+1;
                }
        }
        int maxn = 0;
        for(int i = 0; i < n; ++i)
        {
                if(dp[i] > maxn)
                        maxn = dp[i];
        }
        if(maxn > 1)
        printf("My king, at most %d roads can be built.\n",maxn);
        else
        printf("My king, at most %d road can be built.\n",maxn);
     }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值