真题算法考点

  

  钢板填坑问题
  路面有n个坑,需要用m个钢板盖住
  m个钢板钱不一样,尺寸不一样
  ,固定给出m个钢板,看怎么组合能用总费用最少的钢板盖住所有坑
  例,
  ,2 3
  ,80
  ,50 5 90 80 4
  
  
  结果1 7
  给定2个坑,3个钢板
  ,每个坑的直径
  ,每个钢板的直径和费用且成对出现
  ,最后计算是第一个案例,最少使用的费用是7
  思路是按费用排序,每次最少费用的钢板该直径最大的坑,保证这一个钢板肯定只能盖住这一个坑。这样后面的钢板
  ,也能对应盖一个坑
  ,3
  ,2 3
  ,80
  ,50 5 90 80 4
  ,3 5
  ,50 80 40
  ,50 75 79 70 4 7 40 5
  ,5 10
  ,50 40 50 60 50
  ,50 54 60 11 45 22 49 51 35 16 80 53 70 80 99 90 84 55 23
  给定框架
  包sasst.web;
  进口java.util.Scanner;
  公共类解决方案{
  ,静态整数N, M;
  ,,静态int []=new int[1000]你好,
  ,,静态int Si []=new int [10000];
  ,,静态πint []=new int [10000];
  ,,公共静态void main (String [] args) {
  ,,扫描仪sc=new扫描仪(系统);
  ,,int T=sc.nextInt ();
  ,,for (int test_case=1; test_case<=T; + + test_case) {
  ,,,N=sc.nextInt ();
  ,,,M=sc.nextInt ();
  ,,,(int i=0;我,,嗨,[我]=sc.nextInt ();
  ,,,}
  ,,,(int i=0;我,,如果[我]=sc.nextInt ();
  ,,,π[我]=sc.nextInt ();
  ,,,}
  ,,,System.out.println ();
  ,,system . out。println(“璟规模”);
  ,,,(int i=0;我,,system . out。打印(嗨[我]+ ");
  ,,,}
  ,,,System.out.println ();
  ,,system . out。println (“gangban”);
  ,,,
  ,,,(int i=0;我,,system . out。打印(Si(我)+ " " +π(我)+ " "),
  ,,,}
  ,,,System.out.println ();
  ,,}
  ,,}
  ,}
  
  
  具体实现
  ,注意
  ,前面数组构造时的长度,改成N, M,而不是1000具体值,以防后面比较时数组中出现大量0影响排序结果
  新时候的语句要在输入以后,而不是最上面声明
  进口java.util.Arrays;
  ,进口java.util.Comparator;
  ,进口java.util.Scanner;
  公开课解决{
  ,静态整数N, M;
  ,静态int[]你好;
  ,静态int Si [],
  ,静态πint [],
  ,静态MtDate [] MtDate;
  ,公共静态void main (String [] args) {
  ,,扫描仪sc=new扫描仪(系统);
  ,,int T=sc.nextInt ();
  ,,(int test_case=1; test_case<=T; + + test_case) {
  ,,N=sc.nextInt ();
  ,,M=sc.nextInt ();
  ,,你好=new int [N];
  ,,for (int i=0;我,,嗨,[我]=sc.nextInt ();
  ,,}
  ,,,
  ,,如果=new int [M]。
  ,,π=new int [M]。
  ,,mtDate=new mtDate [M]。
  ,,for (int i=0;我,,如果[我]=sc.nextInt ();
  ,,[我],π=sc.nextInt ();
  ,,,mtDate[我]=new mtDate ();
  ,,,mtDate[我]。如果=如果[我];
  ,,,mtDate[我]。π=π[我];
  ,,}
  ,,比较器太=new我();
  ,,Arrays.sort (mtDate mt);
  ,,Arrays.sort (Hi);
  ,,int价格=0;
  ,,for (int i=n - 1; i>=0;我——){
  ,,,int t=getPrice(嗨[我]);
  ,,,如果价格(t> 0)=价格+ t;
  ,,,其他{价格=1;打破;}
  ,,}
  ,,System.out.println ();
  ,,System.out.print (test_case +”、“+价格),
  ,,,
  ,,,
  ,,}
  ,}
  ,,
  ,,静态int getPrice (int hhi)
  ,{
  ,,(int i=0;我,,如果(mtDate[我].Si>=hhi&及! mtDate[我].used) {
  ,,,mtDate[我].used=true;
  ,,,返回mtDate[我].Pi;
  ,,}
  ,,}
  ,,返回1;
  ,}
  ,}
  类MtDate {
  ,公共int Si,π;
  ,公共使用布尔=false;
  ,,
  ,}
  类我实现比较器{
  ,公共int比较(MtDate o1、o2 MtDate) {
  ,如果(o1群。π,,返回1;
  ,,}else if (o1.Pi> o2.Pi) {
  ,,返回1;
  ,,其他}{
  ,,返回0;
  ,,}
  ,}
  ,}
  
  
  20170713真算法
  查找避难所个数
  1 2 3 7 8 9
  2 9 8 6 5 2
  2 3 2 5 6 7
  1 2 3 2 6 8

真题算法考点