結果

問題 No.43 野球の試合
ユーザー uafr_csuafr_cs
提出日時 2015-06-03 20:28:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 191 ms / 5,000 ms
コード長 2,163 bytes
コンパイル時間 2,482 ms
コンパイル使用メモリ 82,268 KB
実行使用メモリ 58,820 KB
最終ジャッジ日時 2023-09-20 19:01:47
合計ジャッジ時間 4,531 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,848 KB
testcase_01 AC 122 ms
55,676 KB
testcase_02 AC 123 ms
55,672 KB
testcase_03 AC 124 ms
55,892 KB
testcase_04 AC 124 ms
55,772 KB
testcase_05 AC 124 ms
56,056 KB
testcase_06 AC 126 ms
55,732 KB
testcase_07 AC 191 ms
58,820 KB
testcase_08 AC 124 ms
55,568 KB
testcase_09 AC 124 ms
55,932 KB
testcase_10 AC 124 ms
55,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static class Team implements Comparable<Team> {
		int degs, id;

		public Team(int degs, int id) {
			super();
			this.degs = degs;
			this.id = id;
		}

		@Override
		public int compareTo(Team o) {
			if(Integer.compare(o.degs, this.degs) != 0){
				return Integer.compare(o.degs, this.degs);
			}else{
				return Integer.compare(this.id, o.id);
			}
		}
		
		@Override
		public String toString(){
			return this.id + " : " + this.degs;
		}
	}
	
	public static int dfs(final int N, int x, int y, Team[] teams, boolean[][] undecided){
		if(y == N){
			Team[] sorted = new Team[N];
			System.arraycopy(teams, 0, sorted, 0, N);
			
			Arrays.sort(sorted);
			
			int order = 1, prev_degs = sorted[0].degs;
			for(int i = 0; i < N; i++){
				if(sorted[i].degs < prev_degs){
					order++;
					prev_degs = sorted[i].degs;
				}
				
				if(sorted[i].id == 0){
					return order;
				}
			}
			
			return N;
		}else if(!undecided[y][x]){
			return dfs(N, (x == N - 1) ? y + 1 : x + 1, x == N - 1 ? y + 1 : y, teams, undecided);
		}else{
			int ret = Integer.MAX_VALUE;
			{
				teams[y].degs++; teams[x].degs--;
				ret = Math.min(ret, dfs(N, (x == N - 1) ? y + 1 : x + 1, (x == N - 1) ? y + 1 : y, teams, undecided));
				teams[y].degs--; teams[x].degs++;
			}
			{
				teams[y].degs--; teams[x].degs++;
				ret = Math.min(ret, dfs(N, (x == N - 1) ? y + 1 : x + 1, (x == N - 1) ? y + 1 : y, teams, undecided));
				teams[y].degs++; teams[x].degs--;
			}
			
			return ret;
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();

		Team[] teams = new Team[N];
		for(int i = 0; i < N; i++){
			teams[i] = new Team(0, i);
		}
		boolean[][] undecided = new boolean[N][N];
		for(int i = 0; i < N; i++){
			char[] inputs = sc.next().toCharArray();
			
			for(int j = 0; j < N; j++){
				if(inputs[j] == 'o'){
					teams[i].degs++;
					teams[j].degs--;
				}else if(inputs[j] == '-'){
					undecided[i][j] = true;
				}
			}
		}
		
		System.out.println(dfs(N, 0, 0, teams, undecided));
		
	}
	
}
0