- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class P1009 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int m = sc.nextInt();
- int n = sc.nextInt();
- LinkedList<Room> ll ;
- double x;
- while(m!=-1){
- ll = new LinkedList<Room>();
- x = 0;
- for(int i=0;i<n;i++){
- Room r = new Room(sc.nextInt(),sc.nextInt());
- r.v = r.j*1.0/r.f;
- ll.add(r);
- }
- Collections.sort(ll);
- Collections.reverse(ll);
- int i;
- for(i=0;i<n; i++){
- if(ll.get(i).f > m) break;
- m -= ll.get(i).f;
- }
- if(i<n) x = m*1.0/ll.get(i).f;
- double d =0;
- for(int ii=0;ii<i;ii++)
- d += ll.get(ii).j;
- d+= ll.get(i).j * x;
- System.out.printf("%.3f/n",d);
- m = sc.nextInt();
- n = sc.nextInt();
- }
- }
- }
- class Room implements Comparable<Room>{
- int j,f;
- Double v;
- public Room(int j, int f) {
- this.j = j;
- this.f = f;
- }
- public int getJ() {
- return j;
- }
- public void setJ(int j) {
- this.j = j;
- }
- public int getF() {
- return f;
- }
- public void setF(int f) {
- this.f = f;
- }
- @Override
- public int compareTo(Room o) {
- return this.v.compareTo(o.v);
- }
- }
以下的代码TLE,郁闷
- import java.text.DecimalFormat;
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class P1009 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int m = sc.nextInt();
- int n = sc.nextInt();
- LinkedList<Room> ll ;
- while(m!=-1){
- ll = new LinkedList<Room>();
- for(int i=0;i<n;i++){
- Room r = new Room(sc.nextInt(),sc.nextInt());
- if(r.j ==0)
- continue;
- r.v = r.f*1.0/r.j;
- ll.add(r);
- }
- Collections.sort(ll);
- // Collections.reverse(ll);
- double sum =0;
- for(Room r : ll){
- if(r.f<=m){
- m -= r.f;
- sum += r.j;
- }else{
- sum += m *1.0/r.f *r.j;
- break;
- }
- }
- DecimalFormat df = new DecimalFormat();
- df.applyPattern("0.000");
- System.out.println(df.format(sum));
- // System.out.printf("%.3f/n", sum);
- m = sc.nextInt();
- n = sc.nextInt();
- }
- }
- }
- class Room implements Comparable<Room>{
- int j,f;
- Double v;
- public Room(int j, int f) {
- this.j = j;
- this.f = f;
- }
- @Override
- public int compareTo(Room o) {
- return this.v.compareTo(o.v);
- }
- }