zoukankan      html  css  js  c++  java
  • [TopCoder] SRM 578 DIV 2, Wolf In Zoo, Solution

    Problem Statement

         Mr. Pasuterukun is walking along a straight road. He is cautious, because he has heard that there may be some wolves on the road.

    The road consists of N sections. The sections are numbered 0 through N-1, in order. Each section of the road contains at most one wolf.

    You have M additional pieces of information about the positions of the wolves. Each piece of information is an interval of the road that contains at least one wolf. More precisely, for each i between 0 and M-1, inclusive, you are given two integers left[i] and right[i] such that the sections with numbers in the range from left[i] to right[i], inclusive, contain at least one wolf in total.

    You are given two String[]s L and R. The concatenation of all elements of L will be a single space separated list containing the integers left[0] through left[M-1]. R contains all the integers right[i] in the same format.

    Return the number of ways in which wolves can be distributed in the sections of the road, modulo 1,000,000,007.

    Definition

        
    Class: WolfInZooDivTwo
    Method: count
    Parameters: int, String[], String[]
    Returns: int
    Method signature: int count(int N, String[] L, String[] R)
    (be sure your method is public)
        

    Constraints

    - N will be between 1 and 300, inclusive.
    - L and R will contain between 1 and 50 elements, inclusive.
    - Each element of L and R will contain between 1 and 50 characters, inclusive.
    - Each character in L and R will be a digit ('0'-'9') or a space (' ').
    - M will be between 1 and 300, inclusive.
    - The concatenation of all elements of L will be a single space separated list of M integers. The integers will be between 0 and N-1, inclusive, and they will be given without unnecessary leading zeroes.
    - The concatenation of all elements of R will be a single space separated list of M integers. The integers will be between 0 and N-1, inclusive, and they will be given without unnecessary leading zeroes.
    - For each i, the i-th integer in L will be smaller than or equal to the i-th integer in R.

    Examples

    0)
        
    5
    {"0 1"}
    {"2 4"}
    Returns: 27
    There is at least one wolf on the sections 0 through 2, and at least one wolf on the sections 1 through 4.
    1)
        
    10
    {"0 4 2 7"}
    {"3 9 5 9"}
    Returns: 798
    2)
        
    100
    {"0 2 2 7 10 1","3 16 22 30 33 38"," 42 44 49 51 57 60 62"," 65 69 72 74 77 7","8 81 84 88 91 93 96"}
    {"41 5 13 22 12 13 ","33 41 80 47 40 ","4","8 96 57 66 ","80 60 71 79"," 70 77 ","99"," 83 85 93 88 89 97 97 98"}
    Returns: 250671525
    You must first concatenate the elements of L and only then split it into integers. The same holds for R.
    3)
        
    3
    {"1"}
    {"2"}
    Returns: 6
    The following picture shows all possible patterns.


    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.    


    [Thoughts]
    这道题在TopCoder上是1000分的题,所以肯定不是简单的DFS就可以解决的。尤其N的取值是在1~300之间,DFS的话,时间复杂度肯定很高。

    所以,转换个思路来做就简单的多了。
    以test case 2为例

    10
    {"0 4 2 7"}
    {"3 9 5 9"}

    如图所示,这个题目说到底是个集合问题。

    在代码中,只要求出Cs(A)Cs(B)∪Cs(C)∪Cs(D)即可。Cs是取补集的意思。



























  • 相关阅读:
    vsftp关于"550 create directory operation failed"问题解决
    CentOS 5.5 Samba服务器安装总结
    Centos 5.5下安装samba
    iptables里filter表前面几个数字的意思
    Linux误删C基本运行库libc.so.6急救方法
    Linux升级C基本运行库CLIBC
    MySQL的Grant命令
    Apache Options指令详解
    Apache的Order Allow,Deny 详解
    Python 中 open()文件操作的方式
  • 原文地址:https://www.cnblogs.com/codingtmd/p/5078870.html
Copyright © 2011-2022 走看看