- 剛開始拿到這題很懵逼,知道了別人的思路之后開始寫,但是還是遇到很多坑,要求求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
{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;}elseif(map
.get(q
)>=2) {a
[index
++]=q
;}}if(!b
) {Arrays
.sort(a
,0, index
);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 Educational Codeforces Round 49 (Rated for Div. 2) C题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。