codeforces Educational Codeforces Round 49 (Rated for Div. 2) C题

简介: 刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。

刚开始拿到这题很懵逼,知道了别人的思路之后开始写,但是还是遇到很多坑,要求求P2/S最大。p=a b。就是求(a2+ b2 +2ab)/ab最大,也就是a/b +b/a最大。那么题意就很明显了。


但是处理的时候还要注意,刚开始用map存入数据,保存数量大于2的数据。接着就是找最小的,千万不要用数组进行双重循环查找,这样的O(n*n)会爆时,要先排序O(lgn);然后对相邻的遍历比较一遍就可以了O(n)。当数据量很大的时候差距很明显。

附上ac代码(java)


package codeforces504;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/*
 * 贪心 数学 排序
 */
public class testC {
  public static void main(String[] args) throws IOException {
    // TODO 自动生成的方法存根
    StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    in.nextToken();
    int n=(int)in.nval;
    for(int i=0;i<n;i++)
    {
      in.nextToken();int num=(int)in.nval;
      int a[]=new int[num];
      Map<Integer,Integer>map=new HashMap();    
      for(int j=0;j<num;j++)
      {
        in.nextToken();
        int q=(int)in.nval;
        if(map.containsKey(q)) {map.put(q, map.get(q)+1);}
        else map.put(q, 1);
      }
      int index=0;int a2=0;int b2=0;
      boolean b=false;
      for(int q:map.keySet())
      {
        if(map.get(q)>=4) {a2=q;b2=q;b=true;break;}
        else
          if(map.get(q)>=2) {a[index++]=q;}
      }
      if(!b) {
        Arrays.sort(a,0, index);//0-到index-1排序      
        double min=Double.MAX_VALUE;;
      for(int q=0;q<index-1;q++)
      {
          double d=(double)a[q]/(double)a[q+1]+(double)a[q+1]/(double)a[q];
          if(d<min) {min=d;a2=a[q];b2=a[q+1];}      
      }
      }
      out.println(a2+" "+a2+" "+b2+" "+b2);
      out.flush();
    }
  }
}
目录
相关文章
Codeforces Round #192 (Div. 2) (330B) B.Road Construction
要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目。 要求两个城市之间不超过两条道路,那么所有的城市应该是连在一个点上的,至于这个点就很好找了,只要找到一个没有和其他点有道路限制的即可。
40 0
|
机器学习/深度学习 人工智能
Educational Codeforces Round 113 (Rated for Div. 2)C. Jury Meeting
Educational Codeforces Round 113 (Rated for Div. 2)C. Jury Meeting
57 0
Educational Codeforces Round 113 (Rated for Div. 2)A. Balanced Substring
Educational Codeforces Round 113 (Rated for Div. 2)A. Balanced Substring
88 0
|
人工智能 Windows
Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)
Educational Codeforces Round 113 (Rated for Div. 2) C - Jury Meeting (思维 组合数)
97 0
|
机器学习/深度学习
|
人工智能
Educational Codeforces Round 98 (Rated for Div. 2)B-Toy Blocks
You are asked to watch your nephew who likes to play with toy blocks in a strange way. He has n boxes and the i-th box has ai blocks. His game consists of two steps: he chooses an arbitrary box i; he tries to move all blocks from the i-th box to other boxes.
260 0