额这个通不过,超时,问题不明。。有空弄
某餐馆有n张桌子,每张桌子有一个参数:a 可容纳的最大人数; 有m批客人,每批客人有两个参数:b人数,c预计消费金额。 在不允许拼桌的情况下,请实现一个算法选择其中一部分客人,使得总预计消费金额最大
输入描述:
输入包括m+2行。 第一行两个整数n(1 <= n <= 50000),m(1 <= m <= 50000) 第二行为n个参数a,即每个桌子可容纳的最大人数,以空格分隔,范围均在32位int范围内。 接下来m行,每行两个参数b,c。分别表示第i批客人的人数和预计消费金额,以空格分隔,范围均在32位int范围内。
输出描述:
输出一个整数,表示最大的总预计消费金额
输入例子:
3 5 2 4 2 1 3 3 5 3 7 5 9 1 10
输出例子:
20
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sin = new Scanner(System.in);
while (sin.hasNext()) {
String len = sin.nextLine().trim();
String[] words=len.split(" ");
int n=Integer.valueOf(words[0]);
int m=Integer.valueOf(words[1]);
int[] table=new int[n];
len= sin.nextLine().trim();
String[] words1=len.split(" ");
for(int i=0;i<words1.length;i++){
table[i]=Integer.valueOf(words1[i]);
}
Arrays.sort(table);
LinkedList<LinkedList<Integer>> list=new LinkedList<>();
for(int i=0;i<m;i++){
LinkedList<Integer> temp=new LinkedList<>();
for(int j=0;j<2;j++){
temp.add(sin.nextInt());
}
list.add(temp);
}
int count=0;
for(int i=0;i<table.length;i++){
int aaa=-1;
boolean mark=true;
for(int j=0;j<list.size();j++){
if(mark&&list.get(j).get(0)<=table[i]){
aaa=j;
mark=false;
}else if(list.get(j).get(0)<=table[i]&&list.get(j).get(1)>list.get(aaa).get(1)){
aaa=j;
}
}
if(aaa!=-1){
LinkedList<Integer> temp11111=list.get(aaa);
list.remove(aaa);
count+=temp11111.get(1);
}
}
System.out.print(count);
}
}
}