Google中国编程挑战赛第一轮

Google™ Code Jam 中国编程挑战赛第一轮2005年12月19日21时在线举行,3道难度不同的题目,分数各为250、500和1000,难度高的题目分数较高,编码时间75分钟。

Round  1  Problem  250  Statement

You want to buy two neighboring tickets 
in  the first row of the theater so that one of the tickets  is   as  far from the aisles  as  possible.
You will be given a String describing the first row of the theater where 
' . '  represents an empty seat and  ' X '  represents an occupied seat. Your task  is  to  return  the index (from  0 ) of the empty seat that  is  furthest from the aisles (the two ends of the String) and  is  also next to an empty seat. If there are multiple possible seats,  return  the one with the smallest index. Return  - 1   if  there are no seats that satisfy your requirements.
Definition

Class:
TheaterVisit
Method:
chooseSeat
Parameters:
string
Returns:
int
Method signature:
int  chooseSeat( string  row)
(be sure your method 
is   public )

Constraints
-
row will contain between 
1  and  50  characters, inclusive.
-
Each character 
in  row will be either  ' . '  or  ' X ' .

Examples

0 )

" ..... "
Returns: 
2
You can buy either tickets with indexes 
1  and  2  or tickets with indexes  2  and  3 .

1 )

" ...... "
Returns: 
2

2 )

" ..X... "
Returns: 
3
You should buy tickets with indexes 
3  and  4 .

3 )

" .X.X... "
Returns: 
4

4 )

" X.XX.X "
Returns: 
- 1

5 )

" .. "
Returns: 
0

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.

 

Round  1  Problem  500  Statement

A group of vertical blocks are placed densely one after another on the ground. The blocks each have a width of 
1 , but their heights may vary. For example,  if  the heights of the vertical blocks are given  as  { 1 , 5 , 5 , 1 , 6 , 1 }, the configuration would look like the following picture:
    □
 □□ □
 □□ □
 □□ □
 □□ □
□□□□□□
Your task 
is  to reproduce the exact shape of  this  structure  using  some number of non - intersecting rectangles. You will be given a  int [] heights representing the heights of the vertical blocks from left to right. Return the minimal number of rectangles necessary to perform  this  task with the given configuration of blocks.

Definition

Class:
BlockStructure
Method:
cover
Parameters:
int []
Returns:
int
Method signature:
int  cover( int [] heights)
(be sure your method 
is   public )

Constraints
-
heights will have between 
1  and  50  elements, inclusive.
-
Each element of heights will be between 
1  and  1000 , inclusive.

Examples

0 )

{
1 , 5 , 5 , 1 , 6 , 1 }
Returns: 
3
    ◇
 □□ ◇
 □□ ◇
 □□ ◇
 □□ ◇
■■■■■■
We can use rectangles with sizes 6x1, 2x4 and 1x5.

1 )

{
2 , 2 , 2 , 4 , 4 }
Returns: 
2
   ■■
   ■■
□□□■■
□□□■■
We can use a 3x2 rectangle and a 2x4 rectangle.

2 )

{
6 , 6 , 6 , 6 , 6 , 6 }
Returns: 
1
The structure 
is  a rectangle.

3 )

{
71 , 44 , 95 , 16 , 10 , 80 , 12 , 17 , 98 , 61 }
Returns: 
10
It
' s impossible to use less than 10 rectangles.

4 )

{
100 , 100 , 97 , 100 , 100 , 100 , 97 , 98 , 99 , 99 }
Returns: 
5

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.

 

Round  1  Problem  1000  Statement

We have a sequence of integers, and we would like to remove all duplicate elements from 
this  sequence. There may be multiple ways to perform  this  task. For example, given the sequence {  1 2 1 3  }, we could end up with either {  1 2 3  } or {  2 1 3  }  as  the remaining sequence, depending on which duplicate  1  we remove from the original sequence. For  this  problem, we want to  return  the lexicographically first of of all possible remaining sequences. A sequence S1 comes before sequence S2 lexicographically  if  and only  if  S1 has a smaller value than S2 at the lowest index at which the two sequences differ (so,  for  example, {  1 2 3  } comes before {  2 3 1  }).
You will be given a 
int [] sequence. Return a  int [] containing the sequence after all the duplicates are removed. See the examples  for  further clarification.

Definition

Class:
HardDuplicateRemover
Method:
process
Parameters:
int []
Returns:
int []
Method signature:
int [] process( int [] sequence)
(be sure your method 
is   public )

Constraints
-
sequence will have between 
1  and  50  elements, inclusive.
-
Each element of sequence will be between 
1  and  1000 , inclusive.

Examples

0 )

{
5 6 5 1 6 5 }
Returns: {
1 6 5  }
There are six different ways to remove duplicates (remaining numbers are marked by 
' * ' ):
* 5 * 6 ,   5 * 1 ,   6 ,   5 },
* 5 ,   6 ,   5 * 1 * 6 ,   5 },
{  
5 * 6 * 5 * 1 ,   6 ,   5 },
{  
5 ,   6 * 5 * 1 * 6 ,   5 },
{  
5 * 6 ,   5 * 1 ,   6 * 5 },
{  
5 ,   6 ,   5 * 1 * 6 * 5 }.

The last variant 
is  the lexicographically first.

1 )

{
3 2 4 2 4 4 }
Returns: {
3 2 4  }

2 )

{
6 6 6 6 6 6 }
Returns: {
6  }

3 )

{
1 3 2 4 2 3 }
Returns: {
1 2 4 3  }

4 )

{
5 4 1 5 }
Returns: {
4 1 5  }

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.

posted on 2005-12-20 11:03 空间/IV 阅读(16) 评论(3)  编辑 收藏 收藏至365Key 所属分类: 算法C# Base

评论

# tangxin的C#解答 2005-12-20 15:27 空间/IV

 1 public   class  TheaterVisit
 2 {
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1 public   class  BlockStructure
 2 {
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1 using  System.Collections;
 2
 3 public   class  HardDuplicateRemover
 4 {
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}

 1 public   class  TheaterVisit
 2 {
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1 public   class  BlockStructure
 2 {
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1 using  System.Collections;
 2
 3 public   class  HardDuplicateRemover
 4 {
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}
  

# hyyylr的C#解答 2005-12-20 15:29 空间/IV

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}
  

# az的C#解答 2005-12-20 16:28 空间/IV

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}
#  tangxin的C#解答 2005-12-20 15:27 空间/IV

 1 public   class  TheaterVisit
 2 {
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1 public   class  BlockStructure
 2 {
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1 using  System.Collections;
 2
 3 public   class  HardDuplicateRemover
 4 {
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}

 1 public   class  TheaterVisit
 2 {
 3  public int chooseSeat(string row)
 4  {
 5    int r = -1, best = -1, t, l1 = row.Length-1;
 6    for (int i = 0; i < row.Length; i++)
 7    {
 8      if (row[i] == '.' &&
 9        ((i > 0 && row[i-1== '.'|| (i < l1 && row[i+1== '.')) &&
10        (t = System.Math.Min(i, l1-i)) > best)
11      {
12        r = i;
13        best = t;
14      }

15    }

16    return r;
17  }

18}
 1 public   class  BlockStructure
 2 {
 3  int[] hs;
 4
 5  public int cover(int[] heights)
 6  {
 7    hs = heights;
 8    return minimize(0, hs.Length);
 9  }

10
11  int minimize(int left, int right)
12  {
13    int min = hs[left], r = 1, x = left-1;
14    for (int i = left; i < right; i++)
15    {
16      min = System.Math.Min(min, hs[i]);
17    }

18    for (int i = left; i <= right; i++)
19    {
20      if (i == right || hs[i] == min)
21      {
22        if (i != x+1) r += minimize(x+1, i);
23        x = i;
24      }

25    }

26    return r;
27  }

28}
 1 using  System.Collections;
 2
 3 public   class  HardDuplicateRemover
 4 {
 5  public int[] process(int[] s)
 6  {
 7    Hashtable ht = new Hashtable();
 8    for (int i = 0; i < s.Length; i++)
 9    {
10      ht[s[i]] = i;
11    }

12    int[] r = new int[ht.Count];
13    for (int ii = 0, i = 0; i < ht.Count; i++)
14    {
15      int min = int.MaxValue, j = ii, jj = 0;
16      while (j < s.Length)
17      {
18        if (min > s[j] && (int)ht[s[j]] >= 0)
19        {
20          min = s[j];
21          jj = j;
22        }

23        if ((int)ht[s[j]] == j) break;
24        j++;
25      }

26      ii = jj;
27      r[i] = min;
28      ht[min] = -1;
29    }

30    return r;
31  }

32}
  

# hyyylr的C#解答 2005-12-20 15:29 空间/IV

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int r = 0;
 6    for (;;)
 7    {
 8      bool f = false;
 9      for (int i = 0; i < heights.Length; i++)
10      {
11        if (heights[i] > 0)
12        {
13          f = true;
14          int j = i;
15          while (j+1 < heights.Length && heights[j+1> 0) j++;
16          int h = heights[i];
17          for (int k = i; k <= j; k++) h = System.Math.Min(h, heights[k]);
18          ++r;
19          for (int k = i; k <= j; k++) heights[k] -= h;
20          i = j;
21        }

22      }

23      if (!f) break;
24    }

25    return r;
26  }

27}
  

# az的C#解答 2005-12-20 16:28 空间/IV

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}

 1 public   class  BlockStructure
 2 {
 3  public int cover(int[] heights)
 4  {
 5    int count = 0;
 6    bool isSearch = false;
 7    for (int i = 0; i < 1000; i++)
 8    {
 9      for (int j = 0; j < heights.Length; j++)
10      {
11        if (i == heights[j]-1) isSearch = true;
12        if (isSearch && (j == heights.Length-1 || i > heights[j+1]-1))
13        {
14          ++count;
15          isSearch = false;
16        }

17      }

18    }

19    return count;
20  }

21}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值