結果

問題 No.844 split game
ユーザー 37zigen37zigen
提出日時 2020-03-25 15:22:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 3,640 bytes
コンパイル時間 2,470 ms
コンパイル使用メモリ 75,372 KB
実行使用メモリ 65,108 KB
最終ジャッジ日時 2023-08-30 11:40:29
合計ジャッジ時間 17,535 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 181 ms
56,632 KB
testcase_01 AC 177 ms
59,136 KB
testcase_02 AC 403 ms
60,396 KB
testcase_03 AC 229 ms
58,732 KB
testcase_04 AC 155 ms
55,096 KB
testcase_05 AC 310 ms
60,388 KB
testcase_06 AC 151 ms
55,264 KB
testcase_07 AC 153 ms
59,068 KB
testcase_08 AC 114 ms
52,904 KB
testcase_09 AC 314 ms
60,444 KB
testcase_10 AC 326 ms
60,376 KB
testcase_11 AC 325 ms
64,716 KB
testcase_12 AC 310 ms
60,828 KB
testcase_13 AC 167 ms
56,388 KB
testcase_14 AC 179 ms
58,456 KB
testcase_15 AC 343 ms
64,848 KB
testcase_16 AC 337 ms
65,108 KB
testcase_17 AC 338 ms
64,704 KB
testcase_18 AC 379 ms
65,004 KB
testcase_19 AC 336 ms
64,252 KB
testcase_20 AC 438 ms
64,936 KB
testcase_21 AC 337 ms
64,920 KB
testcase_22 AC 337 ms
64,860 KB
testcase_23 AC 82 ms
52,600 KB
testcase_24 AC 88 ms
52,600 KB
testcase_25 AC 86 ms
52,556 KB
testcase_26 AC 58 ms
48,692 KB
testcase_27 AC 93 ms
52,904 KB
testcase_28 AC 55 ms
49,536 KB
testcase_29 AC 85 ms
52,608 KB
testcase_30 AC 88 ms
52,320 KB
testcase_31 AC 85 ms
52,316 KB
testcase_32 AC 55 ms
47,916 KB
testcase_33 AC 91 ms
52,268 KB
testcase_34 AC 80 ms
52,900 KB
testcase_35 AC 108 ms
53,108 KB
testcase_36 AC 88 ms
53,248 KB
testcase_37 AC 121 ms
52,928 KB
testcase_38 AC 102 ms
54,824 KB
testcase_39 AC 130 ms
58,084 KB
testcase_40 AC 102 ms
54,936 KB
testcase_41 AC 45 ms
49,476 KB
testcase_42 AC 45 ms
49,616 KB
testcase_43 AC 45 ms
49,540 KB
testcase_44 AC 45 ms
49,792 KB
testcase_45 AC 45 ms
49,752 KB
testcase_46 AC 45 ms
49,512 KB
testcase_47 AC 45 ms
49,436 KB
testcase_48 AC 45 ms
49,636 KB
testcase_49 AC 45 ms
49,684 KB
testcase_50 AC 45 ms
49,488 KB
testcase_51 AC 45 ms
49,404 KB
testcase_52 AC 44 ms
49,592 KB
testcase_53 AC 44 ms
49,420 KB
testcase_54 AC 45 ms
49,484 KB
testcase_55 AC 45 ms
49,516 KB
testcase_56 AC 46 ms
49,416 KB
testcase_57 AC 44 ms
49,424 KB
testcase_58 AC 46 ms
49,884 KB
testcase_59 AC 46 ms
49,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	void run() {
		FastScanner sc = new FastScanner();
		int N=sc.nextInt();
		int M=sc.nextInt();
		long A=sc.nextLong();
		long[][] dp=new long[N+1][2];//dp[i]:=(i-1,i)を繋ぐ辺が(1:ある,0:ない)まで決定したときの最大値
		for(int i=0;i<dp.length;++i)for(int j=0;j<dp[i].length;++j)dp[i][j]=Long.MIN_VALUE/3;
		long[][] query=new long[M][3];
		for(int i=0;i<M;++i) {
			query[i][0]=sc.nextLong()-1;//l
			query[i][1]=sc.nextLong()-1;//r
			query[i][2]=sc.nextLong();//p
		}
		Arrays.sort(query, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return Long.compare(o1[0], o2[0]);
			}
		});
		int p=0;
		dp[0][1]=0;
		for(int i=0;i<=N;++i) {//dp[i]まで確定
			if(i>0)dp[i][0]=Math.max(dp[i][0], Math.max(dp[i-1][0],dp[i-1][1]));
			if(i>0)dp[i][1]=Math.max(dp[i][1], Math.max(dp[i-1][0],dp[i-1][1])-A);
			while(p<query.length&&query[p][0]<=i) {
				int l=(int)query[p][0],r=(int)query[p][1];
				dp[r+1][1]=Math.max(dp[r+1][1], dp[l][1]-(r+1==N?0:A)+query[p][2]);
				if(l>0)dp[r+1][1]=Math.max(dp[r+1][1], dp[l-1][0]-A-(r+1==N?0:A)+query[p][2]);
				++p;
			}
		}
		System.out.println(Math.max(dp[N][1],dp[N][0]));
	}

	
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
	
	
class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    
    public int nextInt() {
    	return (int)nextLong();
    }
}
0