Google中国编程挑战赛第一轮

2005年12月19日21时,Google Code Jam中国编程挑战赛第一轮在线举行,有3道难度不同的题目,编码时间75分钟。博客给出了部分题目的C#解答代码,还展示了hyyylr和az两人对同一题目的不同C#解答。

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}
内容概要:本文针对配电网在发生故障后的恢复重构问题,提出一种结合改进深度优先搜索(DFS)算法与二进制粒子群优化(BPSO)的混合优化方法,旨在实现快速、高效的供电恢复。通过引入改进的DFS算法,有效校验配电网重构过程中的辐射状拓扑约束,确保网络的连通性与非环性,显著提升了拓扑可行性判断的效率;同时采用BPSO算法进行全局寻优,以最小化停电损失、降低网络损耗、最大化负荷恢复为目标构建综合优化模型,增强了重构方案的经济性与可靠性。该方法在Matlab平台上完成仿真验证,结果表明其在求解速度、收敛稳定性及优化质量方面均优于传统算法,具备良好的实用价值与推广前景,尤其适用于结构复杂的现代配电网在故障条件下的实时自愈控制。; 适合人群:电力系统自动化、电气工程及其自动化等相关专业的高校研究生、科研人员,以及从事配电网运行管理、智能电网开发与故障恢复系统设计的工程技术人员。; 使用场景及目标:①实现配电网故障后的快速网络重构与负荷恢复;②提升配电系统在突发事件下的自愈能力与供电可靠性;③优化开关操作策略,降低运行损耗并缩小停电影响范围;④为智能配电管理系统提供核心算法支撑。; 阅读建议:建议读者结合提供的Matlab代码深入理解改进DFS的拓扑校验逻辑与BPSO的编码机制、适应度函数设计及收敛特性分析,重点关注算法在不同故障场景下的鲁棒性表现,并可根据实际馈线结构进行参数调优与仿真迁移。
内容概要:本文系统阐述了基于Benders分解算法求解混合整数规划中的机组组合问题,重点介绍该方法在电力系统经济调度中的建模思路与优化机制。机组组合作为电力系统运行的核心优化问题,旨在通过合理安排各发电机组的启停状态与出力计划,在满足负荷需求与系统约束的前提下最小化总运行成本。Benders分解算法通过将原问题分解为包含整数变量的主问题(机组启停决策)和包含连续变量的子问题(经济负荷分配),采用迭代方式交替求解,并通过生成Benders割(可行性割与最优性割)逐步逼近全局最优解,从而有效应对大规模机组组合问题的计算复杂性。文中配套提供了完整的Matlab代码实现,涵盖模型构建、算法流程设计、收敛判据设置及结果可视化等环节,有助于深入理解算法在实际电力系统调度中的应用细节。; 适合人群:具备电力系统分析、运筹学基础及Matlab编程能力,从事能源系统优化、电力市场调度或相关方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握Benders分解算法的基本原理及其在混合整数规划中的应用范式;② 实现机组组合问题的数学建模与高效求解,提升对电力系统日前调度与经济运行的理解;③ 借助Matlab代码进行算法复现、性能测试与扩展改进,支持学术研究或实际工程问题的原型验证; 阅读建议:此资源强调理论推导与编程实践的紧密结合,建议读者在熟悉拉格朗日松弛、对偶理论等背景知识的基础上,逐模块调试代码,重点关注主问题与子问题之间的信息交互机制,并尝试引入更多实际约束(如爬坡速率、最小启停时间)以深化对算法鲁棒性与适应性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值