TopCoder 550 points 1-SRM 144 DIV 1 165/550 30%

来源:互联网 发布:安卓小众软件 编辑:程序博客网 时间:2024/06/06 14:19

这道题主要的一个问题是计算,一个N个数组成的序列(比如1到10),从中选取M个数(比如M=2)组成新的序列,要求新的序列是升序,问有几种选法?

我在这道题里用到了递归,比如N=8,M=4,设计一个函数

int  F(int M,  int  N);

如果M==2,直接可以返回N*(N-1),前提是N>1,否则返回0

如果M>2,刚调用一个循环,在循环中调用函数F

for (int i = 1; i <=N; i++)
tt += getTt(
M- 1,N - i);

则递归调用函数F(M-1,N-i)

另外有个问题

按照题目的条件N最大为100,M最大为8

这种情况下,这个函数为调用12亿次左右,速度太慢,

后面我想出一个办法,设计一个二维数组 ttArray[9][101],每次在函数里,根据传入的这两个参数,

先去数组里取数,取到了直接返回,没取到再继续计算,这样效率提高了不少。


Problem Statement

 

In most states, gamblers can choose from a wide variety of different lottery games. The rules of a lottery are defined by two integers (choices andblanks) and two boolean variables (sorted andunique).choices represents the highest valid number that you may use on your lottery ticket. (All integers between 1 andchoices, inclusive, are valid and can appear on your ticket.)blanks represents the number of spots on your ticket where numbers can be written.

The sorted and unique variables indicate restrictions on the tickets you can create. Ifsorted is set to true, then the numbers on your ticket must be written in non-descending order. Ifsorted is set to false, then the numbers may be written in any order. Likewise, ifunique is set to true, then each number you write on your ticket must be distinct. Ifunique is set to false, then repeats are allowed.

Here are some example lottery tickets, where choices = 15 and blanks = 4:

  • {3, 7, 12, 14} -- this ticket is unconditionally valid.
  • {13, 4, 1, 9} -- because the numbers are not in nondescending order, this ticket is valid only ifsorted = false.
  • {8, 8, 8, 15} -- because there are repeated numbers, this ticket is valid only ifunique = false.
  • {11, 6, 2, 6} -- this ticket is valid only if sorted = false and unique = false.

Given a list of lotteries and their corresponding rules, return a list of lottery names sorted by how easy they are to win. The probability that you will win a lottery is equal to (1 / (number of valid lottery tickets for that game)). The easiest lottery to win should appear at the front of the list. Ties should be broken alphabetically (see example 1).

Definition

 Class:LotteryMethod:sortByOddsParameters:String[]Returns:String[]Method signature:String[] sortByOdds(String[] rules)(be sure your method is public)  

Constraints

-rules will contain between 0 and 50 elements, inclusive.-Each element of rules will contain between 11 and 50 characters, inclusive.-Each element of rules will be in the format "<NAME>:_<CHOICES>_<BLANKS>_<SORTED>_<UNIQUE>" (quotes for clarity). The underscore character represents exactly one space. The string will have no leading or trailing spaces.-<NAME> will contain between 1 and 40 characters, inclusive, and will consist of only uppercase letters ('A'-'Z') and spaces (' '), with no leading or trailing spaces.-<CHOICES> will be an integer between 10 and 100, inclusive, with no leading zeroes.-<BLANKS> will be an integer between 1 and 8, inclusive, with no leading zeroes.-<SORTED> will be either 'T' (true) or 'F' (false).-<UNIQUE> will be either 'T' (true) or 'F' (false).-No two elements in rules will have the same name.

Examples

0)  
{"PICK ANY TWO: 10 2 F F","PICK TWO IN ORDER: 10 2 T F","PICK TWO DIFFERENT: 10 2 F T","PICK TWO LIMITED: 10 2 T T"}
Returns: { "PICK TWO LIMITED",  "PICK TWO IN ORDER",  "PICK TWO DIFFERENT",  "PICK ANY TWO" }

The "PICK ANY TWO" game lets either blank be a number from 1 to 10. Therefore, there are 10 * 10 = 100 possible tickets, and your odds of winning are 1/100.

The "PICK TWO IN ORDER" game means that the first number cannot be greater than the second number. This eliminates 45 possible tickets, leaving us with 55 valid ones. The odds of winning are 1/55.

The "PICK TWO DIFFERENT" game only disallows tickets where the first and second numbers are the same. There are 10 such tickets, leaving the odds of winning at 1/90.

Finally, the "PICK TWO LIMITED" game disallows an additional 10 tickets from the 45 disallowed in "PICK TWO IN ORDER". The odds of winning this game are 1/45.

1)  
{"INDIGO: 93 8 T F", "ORANGE: 29 8 F T", "VIOLET: 76 6 F F", "BLUE: 100 8 T T", "RED: 99 8 T T", "GREEN: 78 6 F T", "YELLOW: 75 6 F F"}
Returns: { "RED",  "ORANGE",  "YELLOW",  "GREEN",  "BLUE",  "INDIGO",  "VIOLET" }

Note that INDIGO and BLUE both have the exact same odds (1/186087894300). BLUE is listed first because it comes before INDIGO alphabetically.

2)  
{}
Returns: { }

Empty case

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.     

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class Lottery {private static long[][] ttarray = new long[9][101];private static long[][] tfarray = new long[9][101];public static String[] sortByOdds(String[] rules) {int len = rules.length;if (len == 0)return new String[0];ArrayList<String> list = new ArrayList<String>();String result[] = new String[len];for (int i = 0; i < len; i++) {long sum = 0L;String t[] = rules[i].split(":");String name = t[0];String rule = t[1];if (len == 1) {result[0] = name;return result;}String[] flags = rule.trim().split(" ");int max = Integer.parseInt(flags[0]);int tempmax = max;int num = Integer.parseInt(flags[1]);boolean isSorted = flags[2].equals("T") ? true : false;boolean isUnique = flags[3].equals("T") ? true : false;long ff = 0;long tt = 0;long ft = tempmax;long tf = 0;ff = (long) Math.pow(max, num);for (int j = 1; j < num; j++)ft *= --tempmax;tt = getTt(num, max);tf = getTf(num, max);if (num == 1)sum = max;elsefor (int k = 0; k < num; k++) {if (!isSorted && !isUnique)sum = ff;else if (!isSorted && isUnique)sum = ft;else if (isSorted && !isUnique)sum = tf;elsesum = tt;}list.add(sum + "-" + name);for (int x = 0; x < 9; x++)for (int y = 0; y < 101; y++) {ttarray[x][y] = 0;tfarray[x][y] = 0;}}Collections.sort(list, new Comparator<String>() {public int compare(String s1, String s2) {String array1[] = s1.split("-");String array2[] = s2.split("-");long v1, v3;String v2, v4;v1 = Long.parseLong(array1[0]);v2 = array1[1];v3 = Long.parseLong(array2[0]);v4 = array2[1];if (v1 > v3)return 1;else if (v1 < v3)return -1;else {if (v2.compareTo(v4) > 1)return 1;elsereturn -1;}}});for (int i = 0; i < len; i++)result[i] = list.get(i).split("-")[1];return result;}private static long getTt(int digit, int n) {if (digit < 0)return 0;long t = ttarray[digit][n];if (t != 0)return t;long tt = 0L;if (digit == 2) {if (n == 0 || n == 1)return 0;elsereturn n * (n - 1) / 2;} else {for (int i = 1; i <= n; i++)tt += getTt(digit - 1, n - i);}ttarray[digit][n] = tt;return tt;}private static long getTf(int digit, int n) {if (digit < 0)return 0;long t = tfarray[digit][n];if (t != 0)return t;long tf = 0L;if (digit == 2) {return n * (n + 1) / 2;} else {for (int i = 1; i <= n; i++)tf += getTf(digit - 1, n - i + 1);}tfarray[digit][n] = tf;return tf;}}



原创粉丝点击