結果

問題 No.324 落ちてた閉路グラフ
ユーザー GrenacheGrenache
提出日時 2015-12-17 22:12:00
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,101 bytes
コンパイル時間 3,701 ms
コンパイル使用メモリ 75,692 KB
実行使用メモリ 52,488 KB
最終ジャッジ日時 2023-10-14 12:51:06
合計ジャッジ時間 7,196 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,652 KB
testcase_01 AC 42 ms
49,704 KB
testcase_02 AC 43 ms
49,812 KB
testcase_03 AC 41 ms
49,600 KB
testcase_04 AC 55 ms
51,616 KB
testcase_05 AC 58 ms
51,520 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 69 ms
52,488 KB
testcase_09 AC 69 ms
52,208 KB
testcase_10 AC 70 ms
51,612 KB
testcase_11 AC 82 ms
52,092 KB
testcase_12 AC 70 ms
52,412 KB
testcase_13 AC 67 ms
50,976 KB
testcase_14 AC 47 ms
49,928 KB
testcase_15 AC 48 ms
49,864 KB
testcase_16 AC 41 ms
49,608 KB
testcase_17 AC 42 ms
49,572 KB
testcase_18 AC 42 ms
49,712 KB
testcase_19 AC 42 ms
49,612 KB
testcase_20 AC 41 ms
49,760 KB
testcase_21 AC 46 ms
49,716 KB
testcase_22 AC 41 ms
49,692 KB
testcase_23 AC 40 ms
49,736 KB
testcase_24 AC 41 ms
50,028 KB
testcase_25 WA -
testcase_26 AC 41 ms
49,876 KB
testcase_27 WA -
testcase_28 AC 40 ms
49,684 KB
testcase_29 AC 39 ms
50,116 KB
testcase_30 AC 40 ms
50,032 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;


public class Main_yukicoder324 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        n = sc.nextInt();
        int m = sc.nextInt();
        w = new int[n];
        not = new boolean[n];
        PriorityQueue<Pair> pq = new PriorityQueue<Pair>();
        for (int i = 0; i < n; i++) {
        	w[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
        	pq.add(new Pair(i));
        }

        int cnt = 0;
        while (cnt < n - m) {
        	Pair e = pq.remove();
    		int l = (e.id - 1 + n) % n;
    		int r = (e.id + 1 + n) % n;

        	if ((e.le != not[l]) || (e.re != not[r])) {
        		continue;
        	}

        	not[e.id] = true;
        	cnt++;
        	if (!e.le) {
        		pq.add(new Pair(l));
        	}
        	if (!e.re) {
        		pq.add(new Pair(r));
        	}
        }

        int ret = 0;
        for (int i = 0; i < n; i++) {
        	if (!not[i]) {
        		int l = (i - 1 + n) % n;
        		int r = (i + 1 + n) % n;
        		if (!not[l]) {
        			ret += w[l];
        		}
        		if (!not[r]) {
        			ret += w[i];
        		}
        	}
        }

        pr.println(ret / 2);

        pr.close();
        sc.close();
    }

    private static int n;
    private static int[] w;
    private static boolean[] not;

    private static class Pair implements Comparable<Pair> {
    	int id;
    	int sum;
    	boolean le;
    	boolean re;

    	Pair(int id) {
    		this.id = id;
    		int l = (id - 1 + n) % n;
    		int r = (id + 1 + n) % n;
    		sum = (not[l] ? 0 : w[l]) + (not[r] ? 0 : w[id]);
    		le = not[l];
    		re = not[r];
    	}

		@Override
		public int compareTo(Pair o) {
			return Integer.compare(this.sum, o.sum);
		}
    }

    @SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

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

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

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

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

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0