杭电ACM OJ 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列 O(nlogn)算法详解+回溯暴力解决+动态规划dp优雅解决

来源:互联网 发布:阿里云智能系统 编辑:程序博客网 时间:2024/05/19 01:11

Constructing Roads In JGShining's Kingdom

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


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.
翻译:有一条富人路,有一条穷人路,比方说富人路有3个点,1 2 3
穷人路有3个点 2 3 1
两个点必须数字一样才可以建一条路,但是并不是一一对应就够了的。
我们不允许路的交叉。
所以问你最多可以修几条路。
思路:其实就是求最长上升子序列。
求出两边的最长上升子序列后,求出公共部分的个数就是我们要的答案。
怎么求?有一个不用求最长上升子序列的回溯法,
http://blog.csdn.net/qq_36523667/article/details/78629440
此外 还有动态规划的o(n^2)和o(nlogn)算法
http://blog.csdn.net/qq_36523667/article/details/78631696
都讲的十分详细,这里简单贴一下代码。
回溯法
public class LIS {    //1 2 3    //2 3 1    void print() {        System.out.print(this.max + "");    }    LIS(int a[], int b[]) {        n = a.length;        first = new int[n];        second = new int[n];        for (int i = 0; i < n; i ++) {            first[i] = a[i];            second[i] = b[i];        }    }    //匹配一对后,被匹配组中必须从刚刚那个被匹配的数后开始找    int max;    int n;    int first[];    int second[];    void calculate() {        back(0, 0, 0);    }    //方法1:回溯(暴力法)    void back(int a, int b, int sum) {        //a,b,sum come in with 0,b will put b+1 to back        // 1 2 3        // 2 3 1        if (b == n || a == n) {            max = Math.max(max, sum);            return;        }        int newIndex = -1;        boolean flag = false;        for (int i = b; i < n; i ++) {            //如果找得到,可以连也可以不连            if (first[a] == second[i]) {                newIndex = i;                flag = true;                break;            }        }        //如果找不到        if (!flag) {            //继续找下一个ab不增加,sum不增加            back(a + 1, b, sum);            //如果找到了        } else {            for (int i = 0; i < 2; i ++) {                //选择连                if (i == 0) {                    back(a + 1, newIndex + 1, sum + 1);                    //不选择连                } else {                    back(a + 1, b, sum);                }            }        }    }    public static void main(String[] args) throws Exception {        int[] a = new int[5];        int[] b = new int[5];        a[0] = 5;        a[1] = 1;        a[2] = 2;        a[3] = 3;        a[4] = 4;        b[0] = 3;        b[1] = 2;        b[2] = 1;        b[3] = 4;        b[4] = 5;        LIS lis = new LIS(a, b);        lis.calculate();        lis.print();    }}

动态规划o n^2
public class LIS_Dynamic {    int[] a;    LIS_Dynamic(int...ints) {        int n = ints.length;        a = new int[n];        for (int i = 0; i < n; i ++) {            a[i] = ints[i];        }        int max = dynamic2(a);        System.out.print(max + "");    }    //朴素dp o n^2    int dynamic(int[] a) {        int n = a.length;        int[] dp = new int[n];        dp[0] = 1;        int max = 1;        for (int i = 1; i < n; i ++) {            dp[i] = 1;            for (int j = 0; j < i; j ++) {                if (a[j] < a[i]) {                    dp[i] = Math.max(dp[j] + 1, dp[i]);                } else if (a[j] == a[i]) {                    dp[i] = Math.max(dp[j], dp[i]);                }            }            max = Math.max(max, dp[i]);        }        return max;    }    //朴素dp优化 二分查找 模拟栈 -空间换时间    //二分不难不用学就能写出来,但是要搞明白为什么要这么做    int dynamic2(int[] a) {        int n = a.length;        List<Integer> list = new ArrayList<>();        list.add(a[0]);        for (int i = 1; i < n; i ++) {            int topIndex = list.size() - 1;            if (a[i] > list.get(topIndex)) {                list.add(a[i]);            } else {                int startIndex = 0;                int endIndex = topIndex;                while (true) {//end小于start退出                    int index = (endIndex + startIndex) / 2;                    //对本题而言,不存在相等的情况                    //当前元素比中间数小                    if (list.get(index) > a[i]) {                        endIndex = index - 1;                        if (endIndex == startIndex) {                            if (a[i] < list.get(startIndex)) {                                list.set(startIndex, a[i]);                            } else {                                list.set(index, a[i]);                            }                            break;                        }                        //如果当前元素比中间数大                    } else if (list.get(index) < a[i]) {                        startIndex = index + 1;                        if (startIndex == endIndex) {                            list.set(startIndex, a[i]);                            break;                        }                    }                }            }        }        return list.size();    }    public static void main(String[] args) throws Exception {        LIS_Dynamic lis = new LIS_Dynamic(2,5,3,4,1,7,6);    }}

阅读全文
0 0
原创粉丝点击