TopCoder SRM 718

来源:互联网 发布:2010年流行网络歌曲 编辑:程序博客网 时间:2024/05/21 20:58

除了第一题我用C with STL写的,其他两个是java题解

Problem Statement

The city of Byteland has plans to give the city a new skyline by building n new skyscrapers in a row. The skyscrapers will be numbered 0 through n-1 from left to right. The height of skyscraper i will be h[i]. It is guaranteed that all the heights are distinct.

The height profile of a skyline is a sequence of integers that is produced by writing down the numbers of the skyscrapers in the order of their height, starting from the tallest one. For example, if the heights of the skyscrapers were {50,20,10,40,30}, the height profile would be {0,3,4,1,2}: skyscraper 0 is the tallest one, skyscraper 3 is the second tallest, and so on.

Unfortunately, due to some budget constraints, Byteland can only afford to build n-1 of the planned n skyscrapers. A committee is going to choose which one of the n skyscrapers they won't build. The remaining n-1 skyscrapers will then receive new numbers: 0 through n-2, again going from left to right.

Different choices by the committee may lead to skylines with different height profiles. Compute and return the number of distinct height profiles that can be produced.

Definition

  • ClassRelativeHeights
  • MethodcountWays
  • Parametersvector<int>
  • Returnsint
  • Method signatureint countWays(vector<int> h)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Constraints

  • h will contain between 2 and 50 elements, inclusive.
  • Each element of h will be between 1 and 1,000, inclusive.
  • Elements of h will be distinct.

Test cases

    • h{ 1, 3, 6, 10, 15, 21 }
    Returns1
    The committee can choose one of six different skylines:
    • {3,5,10,15,21}
    • {1,5,10,15,21}
    • {1,3,10,15,21}
    • {1,3,5,15,21}
    • {1,3,5,10,21}
    • {1,3,5,10,15}
    However, all of these skylines have the same height profile: {4,3,2,1,0}.
    • h{ 4, 2, 1, 3 }
    Returns3
    Here, the committee can choose one of four possible skylines: {2,1,3}, {4,1,3}, {4,2,3}, or {4,2,1}. The height profiles of these skylines are {2,0,1}, {0,2,1}, {0,2,1}, and {0,1,2}, respectively. As the second and the third skyline share the same height profile, there are only three distinct height profiles.
    • h{ 6, 2, 352, 43, 5, 44 }
    Returns6
    • h{ 4, 5, 6, 1, 2, 3 }
    Returns2
    • h{ 10, 9, 7, 5, 3, 1, 8, 6, 4, 2 }
    Returns9

    const int maxn = 55;int ary[maxn][maxn];int tmp[maxn];int cmp(int i, int j) {return tmp[i] > tmp[j];}

int countWays(vector<int> h) {int n = h.size();int cnt = 0;for (int i = 0; i < n; ++i) {int cur = 0;for (int j = 0; j < n; ++j) {if (i != j) {ary[cnt][cur] = cur;tmp[cur] = h[j];cur++;}}sort(ary[cnt], ary[cnt] + n - 1, cmp);if (cnt == 0)cnt++;else {int j = 0;for (; j < cnt; ++j) {int k = n - 2;for (; k >= 0; --k) {if (ary[j][k] != ary[cnt][k]) {break;}}if (k < 0) {break;}}if (j == cnt) {cnt++;}}}return cnt;}


Problem Statement

Correct parentheses sequences can be defined recursively as follows:

  • The empty string "" is a correct sequence.
  • If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
  • If "X" is a correct sequence, then "(X)" is a correct sequence.
  • Each correct parentheses sequence can be derived using the above rules.

Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".

You are given two Strings s1 and s2. Each character in these strings is a parenthesis, but the strings themselves are not necessarily correct sequences of parentheses.

You would like to interleave the two sequences so that they will form a correct parentheses sequence. Note that sometimes two different ways of interleaving the two sequences will produce the same final sequence of characters. Even if that happens, we count each of the ways separately. (See Example 0 for a clarification.)

Compute and return the number of different ways to produce a correct parentheses sequence, modulo 10^9 + 7.

Definition

  • ClassInterleavingParenthesisDiv2
  • MethodcountWays
  • ParametersString , String
  • Returnsint
  • Method signatureint countWays(String s1, String s2)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Constraints

  • s1, s2 will each have between 1 and 50 characters, inclusive.
  • Each character of s1, s2 will be either '(' or ')'.

Test cases

    • s1"(()"
    • s2"())"
    Returns19
    The 19 ways are:

    Here, the red characters come from the first sequence, and the blue characters come from the second sequence.
    • s1")"
    • s2"("
    Returns1
    • s1"((((("
    • s2")))))"
    Returns42
    • s1"()(()"
    • s2"))((())"
    Returns10
    • s1"()()()()()()()()()()()()()()()"
    • s2"()()()()()()()()"
    Returns493841617
    Don't forget about the mod.
    • s1"())())))"
    • s2"))(((("
    Returns0
public class InterleavingParenthesisDiv2 {    public int mod = 1000000007;    public int countWays(String s1, String s2) {        int n = s1.length(), m = s2.length();        int[] p1 = new int[n+1], p2 = new int[m+1];        for (int i = 1; i <= n; i++) p1[i] = p1[i-1] + (s1.charAt(i-1) == '(' ? +1 : -1);        for (int i = 1; i <= m; i++) p2[i] = p2[i-1] + (s2.charAt(i-1) == '(' ? +1 : -1);        int[][] dp = new int[n+1][m+1];        dp[0][0] = 1;        for (int i = 1; i <= n; ++i)        if(p1[i] + p2[0] >= 0) dp[i][0] = dp[i - 1][0];        for (int j = 1; j <= m; ++j)        if(p1[0] + p2[j] >= 0) dp[0][j] = dp[0][j - 1];        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= m; j++) {                if (p1[i] + p2[j] < 0) {                    dp[i][j] = 0;                    continue;                }                dp[i][j] = (dp[i-1][j] + dp[i][j-1]) % mod;            }        }        return p1[n] + p2[m] == 0 ? dp[n][m] : 0;    }}

Problem Statement

Chain City consists of n buildings on a straight line. You are given the int[] dist with n-1 elements. The elements of dist are the distances between adjacent buildings, in order.

Citizens can travel along the straight line in both directions. Their speed is one unit of distance per second. You would now like to help them get around the city faster by building k teleporters. Once you do so, it will be possible to travel between any two teleporters in zero time.

For the purpose of this problem, both buildings and teleporters are considered points. Teleporters can be built anywhere on the line, possibly at non-integer coordinates. A teleporter may share its location with a building.

For any pair of buildings X and Y, let d(X,Y) be the shortest time a person will need to get from X to Y (or vice versa). The diameter of Chain City can be defined as the maximum of all values d(X,Y). Your task is to minimize the diameter of Chain City. Find an optimal way to place the teleporters and compute and return the corresponding value of the diameter. (It can be shown that the optimal diameter will always be an integer.)

Definition

  • ClassChainCity
  • MethodfindMin
  • Parametersint[] , int
  • Returnsint
  • Method signatureint findMin(int[] dist, int k)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)256

Constraints

  • dist will contain between 2 and 2,000 elements, inclusive.
  • Each element of dist will be between 1 and 10^6, inclusive.
  • k will be between 2 and |dist|+1, inclusive.

Test cases

    • dist{ 3, 5, 4 }
    • k2
    Returns4
    The city is shown in the following picture:

    The blue points labeled A, B, C, D are the buildings. The two red points show one optimal placement of the two teleporters. For this placement of teleporters we have d(A,B) = 3, d(A,C) = 4, d(A,D) = 4, d(B,C) = 3, d(B,D) = 3, and d(C,D) = 4. The maximum of these values, and thus the diameter of Chain City, is 4. It can be shown that this solution is optimal.
    • dist{ 3, 5, 4 }
    • k3
    Returns3
    The buildings are in the same locations but now we may build three teleporters. One optimal solution is to build them at the same locations as the buildings B, C, and D.
    • dist{ 3, 5, 4 }
    • k4
    Returns0
    The only optimal solution for this test case is to build the four teleporters at the locations of the four buildings.
    • dist{ 1, 1, 100, 1, 1, 1, 100, 1, 100, 1, 1, 1 }
    • k4
    Returns3
    As dist has 12 elements, there are 13 buildings. Let's call them A through M, in order. As we can see, the buildings form four "clusters": A+B+C, D+E+F+G, H+I, and J+K+L+M are four groups of buildings that are close to each other. One optimal placement of teleporters is to place the first one at building B, the second one halfway between E and F, the third one at building H, and the fourth one halfway between K and L.
    • dist{ 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 }
    • k4
    Returns8
    • dist{ 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000 }
    • k6
    Returns5000000
public class ChainCity {    public int findMin(int[] dist, int k) {        int n = dist.length;        int sum = 0;        for (int x : dist) sum += x;        int lo = 0, hi = sum;        while (lo < hi) {            int mid = (lo + hi) / 2;            long curlen = 0;            int nums = 1;            for (int x = 0; nums <= k && x < n; x++) {                if (dist[x] + curlen > mid) {                    nums++;                    curlen = 0;                } else {                    curlen += dist[x];                }            }            if (nums <= k) hi = mid - 1;            else lo = mid+1;            System.out.println(lo);        }        return lo;    }}










原创粉丝点击