题解 | #【模板】整数域三分#

【模板】整数域三分

https://www.nowcoder.com/practice/12e52f8182354b94b7ff4800dc1c0cfc

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

public class Main {
    static int N = (int)(5e5 + 10);
    static long[] k = new long[N], a = new long[N], b = new long[N];
    static int n,l,r,fl,fr,fm;

    static long f(int x, int idx) {
        return Math.abs(k[idx]*x+a[idx])+b[idx];
    }

    static long F(int x) {
        long sum = 0;
        for(int i=1;i<=n;i++) {
            sum+=f(x,i);
        }
        return sum;
    }

    static void solve() {
        n = in.nextInt();
        l = in.nextInt();
        r = in.nextInt();
        for(int i=1;i<=n;i++) {
            k[i] = in.nextLong();
            a[i] = in.nextLong();
            b[i] = in.nextLong();
        }
        while(r-l>2) {
            fm =(r-l)/3;
            fl=l+fm;
            fr=fl+fm;
            if(F(fl)<F(fr)) r=fr;
            else l=fl;
        }
        long ans=(long)9e18;
        for(int i=l;i<=r;i++){
            ans = Math.min(ans,F(i));
        }
        out.println(ans);
    }

    public static void main(String[] args) {
        int T = in.nextInt();
        while(T-->0) {
            solve();
        }
        out.flush();
    }

    static FastReader in = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    static class FastReader {
        static BufferedReader br;
        static StringTokenizer st;

        FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            String str = "";
            while (st == null || !st.hasMoreElements()) {
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                st = new StringTokenizer(str);
            }
            return st.nextToken();
        }

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

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

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-20 18:18
是不是意味着秋招就完蛋了
花不开柳成荫:如果你是Java,是的
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务