HDU 1025 Constructing Roads In JGShining's Kingdom

转载自Phoebus丶的博客


Constructing Roads In JGShining's Kingdom


Time Limit: 2000/1000 MS ( Java /Others)    Memory Limit: 65536/32768 K (Java/Others)


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都是最长的子序列,题中只需要求序列长度, 明白这种处理方法的意思即可!



[cpp]  view plain  copy
  1. /*********************************** 
  2.  
  3.           二分+贪心 
  4.               复杂度 n*logn  
  5. 欢迎交流,还请各位不吝赐教! 
  6.                    
  7. ***********************************/  
  8. #include <iostream>  
  9. #include <iomanip>  
  10. #include <cstring>  
  11. #include <cstdio>  
  12. #include <cstdlib>  
  13. #include <cmath>  
  14. #include <set>  
  15. #include <map>  
  16. #include <list>  
  17. #include <stack>  
  18. #include <deque>  
  19. #include <queue>  
  20. #include <vector>  
  21. #include <algorithm>  
  22. #include <functional>  
  23.   
  24. #define PI acos(-1.0)  
  25. #define eps 1e-10  
  26. #define INF 0x7fffffff  
  27. #define MOD 1000000007  
  28. #define debug(x) cout << "--------------> " << x << endl  
  29.   
  30. typedef __int64 ll;  
  31. typedef long long LL;  
  32. typedef unsigned long long ULL;  
  33.   
  34. using namespace std;  
  35.   
  36. const int MAXN = 500088;  
  37.   
  38. int simulate_stack[MAXN];                               //模拟栈数组  
  39. int poor_rich[MAXN];                                    //连接,下标为poor city编号,存的是与之连接的rich city编号  
  40. int len;                                                  
  41.   
  42. int find_pos(int num, int l, int r)                     //二分查找  
  43. {  
  44.         int mid;  
  45.         while(l <= r)  
  46.         {  
  47.                 mid = (l+r) >> 1;  
  48.                 if(simulate_stack[mid] < num)  
  49.                 {  
  50.                         l = mid+1;  
  51.                 }  
  52.                 else    r = mid-1;  
  53.         }  
  54.         return l;  
  55. }  
  56.   
  57. int main()  
  58. {  
  59.         int n, index, tmp, len;  
  60.         int cnt = 0;  
  61.         while(~scanf("%d", &n))  
  62.         {  
  63.                 cnt++;  
  64.                 len = 1;                           
  65.                 memset(poor_rich, 0, sizeof(poor_rich));  
  66.                 memset(simulate_stack, 0, sizeof(simulate_stack));  
  67.   
  68.                 for(int i = 0; i < n; ++i)  
  69.                 {  
  70.                         scanf("%d%d", &index, &tmp);  
  71.                         poor_rich[index] = tmp;  
  72.                 }  
  73.                 simulate_stack[1] = poor_rich[1];      //先将poor_city1入栈  
  74.                 for(int i = 2; i <= n; ++i)              
  75.                 {  
  76.                         if(simulate_stack[len] < poor_rich[i])                //小于栈顶元素,直接入栈,长度len++  
  77.                         {  
  78.                                 len++;  
  79.                                 simulate_stack[len] = poor_rich[i];  
  80.                         }  
  81.                         else  
  82.                         {  
  83.                                 int pos = find_pos(poor_rich[i], 0, len);     //大于栈顶元素,找栈中第一个大于该元素的元素并替换  
  84.                                 simulate_stack[pos] = poor_rich[i];  
  85.                         }  
  86.                 }  
  87.                 if(len > 1)  
  88.                 printf("Case %d:\nMy king, at most %d roads can be built.\n\n", cnt, len);  
  89.                 else  
  90.                 printf("Case %d:\nMy king, at most %d road can be built.\n\n", cnt, len);  
  91.         }  
  92. }  





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

[cpp]  view plain  copy
  1. /************************************************* 
  2.                    dp法 
  3.                               复杂度n^2 
  4. *************************************************/  
  5.   
  6. #include <iostream>  
  7. #include <iomanip>  
  8. #include <cstring>  
  9. #include <cstdio>  
  10. #include <cstdlib>  
  11. #include <cmath>  
  12. #include <set>  
  13. #include <map>  
  14. #include <list>  
  15. #include <stack>  
  16. #include <deque>  
  17. #include <queue>  
  18. #include <vector>  
  19. #include <algorithm>  
  20. #include <functional>  
  21.   
  22. #define PI acos(-1.0)  
  23. #define eps 1e-10  
  24. #define INF 0x7fffffff  
  25. #define MOD 1000000007  
  26. #define debug(x) cout << "--------------> " << x << endl  
  27.   
  28. typedef __int64 ll;  
  29. typedef long long LL;  
  30. typedef unsigned long long ULL;  
  31.   
  32. using namespace std;  
  33.   
  34. int dp[500088];                           //dp[i]表示从i开始修路最多可修路的总数  
  35. int poor_rich[500088];  
  36.   
  37. int main()  
  38. {  
  39.      int n, index;  
  40.      while(~scanf("%d", &n))  
  41.      {  
  42.         index = 0;  
  43.         memset(dp, -1, sizeof(dp));  
  44.         memset(poor_rich, 0, sizeof(poor_rich));  
  45.         for(int i = 0; i < n; ++i)  
  46.         {  
  47.                 scanf("%d%d", &index, &poor_rich[index++]);  
  48.         }  
  49.         for(int i = 0; i < n; ++i)  
  50.         {  
  51.                 dp[i] = 1;  
  52.                 for(int j = 0; j < i; ++j)  
  53.                 {  
  54.                         if(poor_rich[j] < poor_rich[i] && dp[j]+1 > dp[i])  
  55.                                 dp[i] = dp[j]+1;  
  56.                 }  
  57.         }  
  58.         int maxn = 0;  
  59.         for(int i = 0; i < n; ++i)  
  60.         {  
  61.                 if(dp[i] > maxn)  
  62.                         maxn = dp[i];  
  63.         }  
  64.         if(maxn > 1)  
  65.         printf("My king, at most %d roads can be built.\n",maxn);  
  66.         else  
  67.         printf("My king, at most %d road can be built.\n",maxn);  
  68.      }  
  69.      return 0;  
  70. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值