AOE还是单体? 题解
AOE还是单体?
https://ac.nowcoder.com/acm/contest/5600/A
考点:贪心
我们可以用性价比来看。
如果我们花费x能打掉大于x的血量,我们肯定优先选择用第二种方法呀
反之我们就用第一种方法。
import java.util.*; import java.math.*; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.PrintWriter; public class Main { public static HashMap<Integer,Long>map = new HashMap<>(); 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; in.nextToken(); int x = (int)in.nval; int num[] = new int[n]; for(int i=0;i<n;i++) { in.nextToken(); num[i] = (int)in.nval; } Arrays.sort(num); long sum=0; if(n>x) { sum=x*num[n-x-1]; for(int i=n-x;i<n;i++) sum+=num[i]; sum-=(x*num[n-x-1]); out.print(sum); } else{ for(int i=0;i<n;i++) sum+=num[i]; out.print(sum); } out.flush(); } }