[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.

wolfinzoo2.png

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"}

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

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



























转载于:https://www.cnblogs.com/codingtmd/archive/2013/05/14/5078870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值