洛谷 差分模板提 P2367 语文成绩 JAVA读入优化

给定N个学生的初试成绩,老师会给第x个学生到第y个学生加z分。问最终学生成绩最小的是多少。

直接差分,最后一个点一直MLE,最后百度出JAVA读入优化过了

import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.math.*;
import java.io.*;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        int n,p;
        int x,y,z;
        st.nextToken();
        n = (int) st.nval;
        int c[] = new int [n + 5];
        st.nextToken();
        p = (int) st.nval;
        int tmp1,tmp2;
        tmp1 = 0;
        for(int i = 1; i <= n; i++){
            st.nextToken();
            tmp2 = (int) st.nval;
            if(i == 1) {
                c[i] = tmp2;
                tmp1 = tmp2;
                continue;
            }
            c[i] = tmp2 - tmp1;
            tmp1 = tmp2;
        }
        for(int i = 0; i < p; i++){
            st.nextToken();
            x = (int) st.nval;
            st.nextToken();
            y = (int) st.nval;
            st.nextToken();
            z = (int) st.nval;
            c[x] += z;
            c[y+1] -= z;
        }
        int Min = c[1];
        for(int i= 2;i <= n;i++){
            c[i] += c[i-1];
            if(c[i]< Min) Min=c[i];
        }
        System.out.println(Min);
    }
}

猜你喜欢

转载自www.cnblogs.com/hznumqf/p/13190907.html