0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC370A~Dの解答[Java]

Posted at

はじめに

今回はコンテスト中にDまで解けたのでそれを載せようと思います。

では、見てきましょう。

A - Raise Both Hands

問題文はこちら

Invalidかどうかは$L$と$R$のxorが$0$かどうかで判定しました。

A.java
final class Main{
	private static final boolean autoFlush = false;
	private static final SimpleScanner sc = new SimpleScanner(System.in);
	private static final SimpleWriter out = new SimpleWriter(System.out,autoFlush);

	public static void main(String[] args){

		int L = sc.nextInt();
		int R = sc.nextInt();
		out.println((L^R)==0?"Invalid":L==1?"Yes":"No");

		out.close();
	}
}

B - Binary Alchemy

問題文はこちら

問題文の通りにシミュレーションしました。

B.java
final class Main{
	private static final boolean autoFlush = false;
	private static final SimpleScanner sc = new SimpleScanner(System.in);
	private static final SimpleWriter out = new SimpleWriter(System.out,autoFlush);

	public static void main(String[] args){

		int N = sc.nextInt();
		int[][] A = new int[N+1][N+1];
		for(int i=1;i<=N;i++)
			for(int j=1;j<=i;j++)
				A[i][j] = sc.nextInt();
		int now = 1;
		for(int i=1;i<=N;i++)
			now = A[Math.max(now,i)][Math.min(now,i)];
		out.println(now);

		out.close();
	}
}

C - Word Ladder

問題文はこちら

辞書順で前の方になる変更は先頭から、後ろの方になる変更は末尾から処理していくことが解が求まります。

C.java
final class Main{
	private static final boolean autoFlush = false;
	private static final SimpleScanner sc = new SimpleScanner(System.in);
	private static final SimpleWriter out = new SimpleWriter(System.out,autoFlush);

	public static void main(String[] args){

		char[] S = sc.nextCharArray();
		char[] T = sc.nextCharArray();
		int N = S.length;
		ArrayList<String> X = new ArrayList<>();
		Loop:while(!Arrays.equals(S,T)){
			int index = 0;
			for(int i=0;i<N;i++){
				if(S[i]!=T[i]){
					index = i;
					if(S[i]>T[i]){
						S[i] = T[i];
						X.add(String.valueOf(S));
						continue Loop;
					}
				}
			}
			S[index] = T[index];
			X.add(String.valueOf(S));
		}
		out.println(X.size());
		out.println(X,"\n");

		out.close();
	}
}

D - Cross Explosion

問題文はこちら

各行・列でブロックを管理して、シミュレーションすることで解きました。

D.java
final class Main{
	private static final boolean autoFlush = false;
	private static final SimpleScanner sc = new SimpleScanner(System.in);
	private static final SimpleWriter out = new SimpleWriter(System.out,autoFlush);

	public static void main(String[] args){

		int H = sc.nextInt();
		int W = sc.nextInt();
		ArrayList<TreeSet<Integer>> RList = new ArrayList<>();
		ArrayList<TreeSet<Integer>> CList = new ArrayList<>();
		for(int i=0;i<=H;i++)
			RList.add(new TreeSet<>());
		for(int i=0;i<=W;i++)
			CList.add(new TreeSet<>());
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				RList.get(i+1).add(j+1);
				CList.get(j+1).add(i+1);
			}
		}
		int Q = sc.nextInt();
		while(Q-->0){
			int R = sc.nextInt();
			int C = sc.nextInt();
			if(RList.get(R).contains(C)){
				RList.get(R).remove(C);
				CList.get(C).remove(R);
				continue;
			}
			Integer key;
			if((key=RList.get(R).floor(C))!=null){
				RList.get(R).remove(key);
				CList.get(key).remove(R);
			}
			if((key=RList.get(R).ceiling(C))!=null){
				RList.get(R).remove(key);
				CList.get(key).remove(R);
			}
			if((key=CList.get(C).floor(R))!=null){
				RList.get(key).remove(C);
				CList.get(C).remove(key);
			}
			if((key=CList.get(C).ceiling(R))!=null){
				RList.get(key).remove(C);
				CList.get(C).remove(key);
			}
		}
		int ans = 0;
		for(TreeSet<Integer> set:RList)
			ans += set.size();
		out.println(ans);

		out.close();
	}
}

感想

A,B:易しめ
C:実装に悩んだ
D:もう少し綺麗に実装したかった…
って感じでした。

う~ん…E解きたかったなぁ…。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?