結果
| 問題 |
No.324 落ちてた閉路グラフ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-12-17 22:12:00 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,101 bytes |
| コンパイル時間 | 4,106 ms |
| コンパイル使用メモリ | 79,316 KB |
| 実行使用メモリ | 39,096 KB |
| 最終ジャッジ日時 | 2024-09-16 07:39:45 |
| 合計ジャッジ時間 | 7,761 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 23 WA * 11 |
ソースコード
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);
}
}
}