QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#406143#6525. New HousesChongQYTL 38ms52476kbJava85.9kb2024-05-06 21:03:532024-05-06 21:03:55

Judging History

你现在查看的是最新测评结果

  • [2024-05-06 21:03:55]
  • 评测
  • 测评结果:TL
  • 用时:38ms
  • 内存:52476kb
  • [2024-05-06 21:03:53]
  • 提交

answer

import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static Read scanner = new Read(System.in);
    public static final long MOD = (long) (1e9 + 7);
    public static final int MID = (int) (5 * 1e5 + 7);
    public static final long LMAX = Long.MAX_VALUE;
    public static final long LMIN = Long.MIN_VALUE;
    public static final int IMAX = Integer.MAX_VALUE;
    public static final int IMIN = Integer.MIN_VALUE;
//    public static long[] arr;
    public static long[] a = new long[MID];
    public static long[] b = new long[MID];
    //    public static long[] Qz = new long[MID];
//    public static long[] Hz = new long[MID];
    public static int N, M , n , m;
    public static long ans;
//    public static boolean[][] stack = new boolean[MID][MID];

    //四个方向
    public static int[] row = {1, -1, 0, 0};
    public static int[] cos = {0, 0, 1, -1};
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static void solve(){
        int n = scanner.nextInt();//人
        int m = scanner.nextInt();//屋子
        long t = m - n;//空着的房子
        for(int i = 1 ; i <= n ; i++){
            a[i] = scanner.nextLong();
            b[i] = scanner.nextLong();
        }
        if(t == 0){
            ans = 0;
            for(int i = 1 ; i <= n ; i++) ans += a[i];
            System.out.println(ans);
            return;
        }
        long cnt;
        if(Math.ceil(m / 2.0) <= t) cnt = n;
        else cnt = t;
        int ct = 0;
        for(int i = 1 ; i <= n  ; i++){
            if(a[i] < b[i]) ct++;
        }
        ans = 0;
        if(ct <= cnt){
            if(ct != n - 1){
                for(int i = 1 ; i <= n ; i++) ans += Math.max(a[i] , b[i]);
            }
        } else {
            if(ct != n - 1){
                ans = 0;
                ArrayList<way> list=  new ArrayList<>();
                for(int i = 1 ; i <= n ; i++) list.add(new way(a[i] , b[i]));
                Collections.sort(list, new Comparator<way>() {
                    @Override
                    public int compare(way o1, way o2) {
                        return Long.compare(o2.y - o2.x , o1.y - o1.x);
                    }
                });
                for(int i = 0 ; i < n ; i++){
                    if(i < cnt){
                        ans += list.get(i).y;
                    } else {
                        ans += list.get(i).x;
                    }
                }
            }

        }

        if(ct == n - 1){
            long num = 0;
            for(int i = 1 ; i <= n ; i++) num += b[i];
            ans = num;
            if(n == 2){
                if(m > 2) {ans = Math.max(a[1] + a[2] , b[1] + b[2]);}
            } else if(n == 1){
                ans = b[1];
            } else {
                ArrayList<way> list=  new ArrayList<>();
                for(int i = 1 ; i <= n ; i++) list.add(new way(a[i] , b[i]));
                Collections.sort(list, new Comparator<way>() {
                    @Override
                    public int compare(way o1, way o2) {
                        return Long.compare(o2.y - o2.x , o1.y - o1.x);
                    }
                });
                for(int i =  1 ; i <= n ; i++) ans += b[i];
                num = 0;
                for(int i = 1 ; i <= n - 2 ; i++) num += a[i];
                num += b[n] + b[n - 1];
                ans = Math.max(ans , num);
            }
        }

        System.out.println(ans);
/*
3
4 5
1 100
100 1
100 1
100 1
2 2
1 10
1 10
2 3
100 50
1 1000
 */






    }

    public static void main(String[] args) {

//        long start = System.currentTimeMillis();//程序开始时间戳
        int T = scanner.nextInt();
        while (T-- != 0) {
            solve();
        }
        out.flush();
//        long end = System.currentTimeMillis();//程序结束时间戳
//        out.println("运行时间 : " + (end - start));//输出运行时间
    }

    //快读
    static class Read {
        BufferedReader br;
        StringTokenizer st;

        public Read(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in), 16384);
            eat("");
        }

        public void eat(String s) {
            st = new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            } catch (IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while (!st.hasMoreTokens()) {
                String s = nextLine();
                if (s == null)
                    return false;
                eat(s);
            }
            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

        public BigDecimal nextBigDecimal() {
            return new BigDecimal(next());
        }
    }
    public static void swap(long[] arr , int index1 , int index2){
        long t = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = t;
    }
    public static long Dot_pitch(long x , long y , long xx , long yy){
        return (xx - x) * (xx - x) + (yy - y) * (yy - y);
//        return (long) Math.sqrt(Math.pow(xx - x , 2) + Math.pow(yy - y , 2));
    }
}

class way {
    public long x , y;

    public way(long x, long y) {
        this.x = x;
        this.y = y;
    }
}

详细

Test #1:

score: 100
Accepted
time: 38ms
memory: 52476kb

input:

3
4 5
1 100
100 1
100 1
100 1
2 2
1 10
1 10
2 3
100 50
1 1000

output:

400
2
1050

result:

ok 3 number(s): "400 2 1050"

Test #2:

score: -100
Time Limit Exceeded

input:

100000
6 11
191141536 365120521
799679686 648574232
102602909 467685128
405440859 796808887
384858152 191995380
433392826 195648471
5 13
831367906 510447872
795639287 575551283
811207605 176441088
240156289 946977042
133416463 721098873
5 5
806744021 699586200
630510306 637827160
49223781 641709297
...

output:

3247545200
4106290713
2653993029
5122532137
5570513606
4063775648
2044500908
1857678917
6815058419
2237593918
6646615756
5638337819
3690874076
5497726904
5513905900
5404435094
4705403467
2411992217
3430587752
5098767681
3921151709
2543899920
2692878616
3833748807
4819569838
850381192
6464787173
8839...

result: