Vector排序

来源:互联网 发布:java web实训项目总结 编辑:程序博客网 时间:2024/05/16 12:32


点击打开链接


import java.io.*;import java.util.Collections;import java.util.Scanner;import java.util.Vector;public class Main {          static  Vector<Node> lisg = new Vector<Node>() ;     static  Vector<Integer>  ans = new Vector<Integer>() ;      public  static  void  main(String [] args){       int n ,  k ;       Scanner sc = new Scanner(System.in) ;           while(sc.hasNext()){           lisg.clear();            n = sc.nextInt() ;           k = sc.nextInt() ;           for(int i = 1 ; i <= n ; i++)  lisg.add(new Node(i , sc.nextInt())) ;               Collections.sort(lisg) ;                              ans.clear() ;                int  sum = 0 ;                for(int i = 0 ; i < lisg.size()  && sum + lisg.get(i).c <= k ; i++){                     ans.add(lisg.get(i).idx) ;                     sum += lisg.get(i).c ;               }               System.out.println(ans.size()) ;               if(ans.size() > 0){                     System.out.print(ans.get(0) );                  for(int i = 1 ; i < ans.size() ; i++) System.out.print(" " + ans.get(i)) ;                 System.out.println() ;               }       }   }}class   Node implements Comparable<Node>{    public int  idx ;    public int  c  ;       public Node(){}    public Node(int idx , int c){       this.idx = idx ;       this.c   =  c ;    }    public int compareTo(Node other){return  this.c - other.c ;}}


0 0