海贼007

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

【题意】三人一组的竞赛,每组的战力由最强两人决定,求出第一组的战力排名的最差情况。也就是要找出尽量多的战力比第一组强的队伍。

【算法】贪心算法

1.把剩下的数排序。
2.从最大的数开始。
    1)第一组队伍的战斗力是A,count=0;
    2)取队列中最大的数x,最小的数y,令a=A-x+1,找一个最小的z>=a,形成一组战队x,y,z,战斗力为x+z。从队列中去掉x,y,z,count++;如果找不到z,则return count+1;

【Java代码】来自菜鸟

 1 import java.util.*;
 2 import java.util.regex.*;
 3 import java.text.*;
 4 import java.math.*;
 5 
 6 
 7 public class TeamContestEasy
 8 {
 9     public int worstRank(int[] strength)
10     {
11         int A,count;
12         
13         //get A
14         A = Math.max(strength[0], strength[1])+Math.max(strength[2], Math.min(strength[0], strength[1]));
15         
16         //count
17         ArrayList<Integer> list = new ArrayList<Integer>();
18         for(int i=3;i<strength.length;i++){
19             list.add(strength[i]);
20         }
21         Collections.sort(list);
22         
23         int temp;
24         boolean getZ;
25         count=0;
26         while(list.size()>0){
27             
28             getZ=false;
29             temp=list.get(list.size()-1);
30             list.remove(list.size()-1);//remove max
31             list.remove(0);//remove min
32             
33             for(int j=0;j<list.size();j++){
34                 if(list.get(j)>(A-temp)){
35                     list.remove(j);//remove z
36                     getZ=true;
37                     break;
38                 }
39             }
40             if(getZ){
41                 count++;
42             }else{
43                 break;
44             }
45         }
46         
47         return count+1;
48     }
49 
50 }
51 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
View Code

【改进版代码】

【分析】Collections.binarySearch 还是挺好用的,就是如果没有找到temp时的返回值(-(insertion point)-1)需要处理一下。

 1 import java.util.*;
 2 import java.util.regex.*;
 3 import java.text.*;
 4 import java.math.*;
 5 
 6 
 7 public class TeamContestEasy
 8 {
 9     public int worstRank(int[] strength)
10     {
11         int A,count;
12         
13         //get A
14         A = Math.max(strength[0], strength[1])+Math.max(strength[2], Math.min(strength[0], strength[1]));
15         
16         //count
17         ArrayList<Integer> list = new ArrayList<Integer>();
18         for(int i=3;i<strength.length;i++){
19             list.add(strength[i]);
20         }
21         Collections.sort(list);
22         
23         int temp,j;
24         count=0;
25         while(list.size()>0){
26             temp=A-list.get(list.size()-1)+1;
27             list.remove(list.size()-1);//remove max
28             list.remove(0);//remove min
29 
30             j = Collections.binarySearch(list, temp);
31             if(j<0){
32                 j=-1*j - 1;
33             }
34             
35             if(j<list.size()){
36                 count++;
37                 list.remove(j);
38             }else{
39                 break;
40             }
41         }
42         
43         return count+1;
44     }
45 }
46 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
View Code

【Java代码】来自大神

【分析】我们的算法差不多,学习了binarySearch的用法。

import java.util.ArrayList; 
import java.util.Collections; 

public class TeamContestEasy { 

  public int worstRank(int[] s) { 
    int ans = 1; 
    int n = s.length - 3; 
    int me = s[0] + s[1] + s[2] - Math.min(s[0], Math.min(s[1], s[2])); 
    ArrayList<Integer> a = new ArrayList<Integer>(100); 
    for (int i = 0; i < n; i++) 
      a.add(s[i + 3]); 
    Collections.sort(a); 
    while (!a.isEmpty()) { 
      n = a.size(); 
      int f = a.get(n - 1); 
      if (f + a.get(n - 2) <= me) 
        break; 
      int key = me - f + 1; 
      int index = Collections.binarySearch(a, key); 
      if (index < 0) { 
        index *= -1; 
        index--; 
      } 
      ans++; 
      if (index == n - 1 || index == n - 2) { 
        a.remove(a.size() - 1); 
        a.remove(a.size() - 1); 
        a.remove(0); 
      } else { 
        a.remove(index); 
        a.remove(0); 
        a.remove(a.size() - 1); 
      } 
    } 
    return ans; 
  } 
}
View Code

【C++代码】来自大神

【分析】他没有删除元素,通过l,r变量来控制剩余可用元素的区域。

#line 5 "TeamContestEasy.cpp" 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <cmath> 
#include <cstdio> 
#include <iostream> 
#include <sstream> 
#include <set> 
using namespace std; 
bool cmp(int a,int b) 
{ 
    return a>b; 
} 
class TeamContestEasy { 
  public: 
  int worstRank(vector <int> strength) { 
    int a[50], i, j, p, s, l, r, ans = 1; 
    p = strength[0]+strength[1]+strength[2]; 
    s = strength[0]; 
    s =min(s,strength[1]); 
    s =min(strength[2],s); 
    p -=s; 
    j = 0; 
    for(i = 3; i< strength.size(); i++) 
    a[j++] = strength[i]; 
    sort(a,a+j,cmp); 
    l = 0; 
    r = (j*2)/3 - 1; 
    while(l < r) 
    { 
        if (a[l] + a[r] > p) 
        { 
            ans ++; 
            l++; 
            r--; 
        } else r--; 
    } 
    return ans; 
  } 


}; 


// Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor
View Code

【总结】:利用贪心算法,这道题还是很简单。

posted on 2013-07-28 22:03  wzhscript  阅读(273)  评论(0编辑  收藏  举报