結果

問題 No.489 株に挑戦
ユーザー tanzaku
提出日時 2017-02-24 21:09:05
言語 Java
(openjdk 23)
結果
AC  
実行時間 182 ms / 1,000 ms
コード長 3,192 bytes
コンパイル時間 5,725 ms
コンパイル使用メモリ 86,032 KB
実行使用メモリ 42,240 KB
最終ジャッジ日時 2024-07-19 22:51:18
合計ジャッジ時間 9,936 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class _489_Validate {
	public static void main(String[] args) throws IOException {
		new _489_Validate().solve();
	}

	void solve() throws IOException {
		try (final InputValidator in = new InputValidator(System.in)) {
//		try (final Scanner in = new Scanner(System.in)) {
			int n = in.nextInt(1, 100000);
			in.space();
			int d = in.nextInt(1, n);
			in.space();
			int k = in.nextInt(1, 1000000);
			in.newLine();
			int[] x = new int[n];
			for (int i = 0; i < n; i++) {
				x[i] = -in.nextInt(1, 1000000);
				in.newLine();
			}
			SlideMinimum slide = new SlideMinimum(x, d + 1);
			int l = 0, r = 0;
			long ans = 0;
			for (int i = n - 1; i >= 0; i--) {
//				dump(i, slide.min(i), ans, -slide.min(i) + x[i]);
				final int v = -slide.min(i) + x[i];
				if (ans <= v) {
					ans = v;
					l = i;
					r = slide.minIdx(i);
				}
			}
			System.out.println(ans * k);
			if (ans != 0) {
				System.out.println(l + " " + r);
			}
		}
	}
	
	// [i,i+m)
	static public class SlideMinimum {
		int[] b;
		int[] idx;
		
		public SlideMinimum(int[] a, int m) {
			final int n = a.length;
			b = new int[n];
			idx = new int[n];
			
			int s, t;
			s = t = 0;
			int[] deq = new int[n];
			for (int i = 0; i < n + m - 1; i++) {
				while( s < t && i < n && a[deq[t-1]] > a[i]) t--;
				if (i < n) deq[t++] = i;
				if (i - m + 1 >= 0) {
					b[i-m+1] = a[idx[i-m+1] = deq[s]];
					if(deq[s] == i - m + 1) {
						s++;
					}
				}
			}
		}
		
		// min [idx..idx+m)
		public int min(int idx) { return b[idx]; }
		public int minIdx(int i) { return idx[i]; }
	}
	
	// for debug
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
	
	static class InputValidator implements Closeable {
		final InputStream in;
		
		final int NO_BUFFER = -2;
		int buffer;
		
		public InputValidator(final InputStream in) {
			this.in = in;
			buffer = NO_BUFFER;
		}
		
		int read() throws IOException {
			final int res = buffer == NO_BUFFER ? in.read() : buffer;
			buffer = NO_BUFFER;
			return res;
		}
		
		void unread(int ch) throws IOException {
			buffer = ch;
		}
		
		// [low, high]
		long nextLong(long low, long high) throws IOException {
			long val = 0;
			int ch = -1;
			while (true) {
				ch = read();
				if (!Character.isDigit(ch)) break;
				val = val * 10 + ch - '0';
				check(val <= high);
			}
			check(ch >= 0);
			check(val >= low);
			unread(ch);
			return val;
		}
		int nextInt(int low, int high) throws IOException { return (int)nextLong(low, high); }
		int[] nextInts(int n, int low, int high) throws IOException {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt(low, high);
				if (i + 1 != n) space();
			}
			newLineOrEOF();
			return res;
		}
		
		void space() throws IOException { int ch = read(); check(ch == ' '); }
		void newLine() throws IOException {
			int ch = read();
			if (ch == '\r') ch = read();
			check(ch == '\n');
		}
		void newLineOrEOF() throws IOException { int ch = read(); check(ch == '\r' || ch == '\n' || ch < 0); }
		void check(boolean b) { if (!b) throw new RuntimeException(); }

		@Override
		public void close() throws IOException {
		}
	}
}
0