1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Tenka1 Programmer Beginner Contest 2019 - A&B&C

Last updated at Posted at 2019-04-20

Tenka1 Programmer Beginner Contest 2019 A&B&C

Tenka1 Programmer Beginner Contest 2019

【解説放送:Tenka1 Programmer Contest 2019 解説配信】

  • サイトにリンクがないためここに貼っておく

A問題

  • 「CがAとBの間にある」なので比較する
	private void solveA() {
		int numA = nextInt();
		int numB = nextInt();
		int numC = nextInt();

		if (numA <= numC && numC <= numB) {
			out.println("Yes");
		} else if (numB <= numC && numC <= numA) {
			out.println("Yes");
		} else {
			out.println("No");
		}

		out.println("");
	}

B問題

  • SのK番目を抜き出しておいて、新しい文字列を作成する
	private void solveB() {
		int numN = nextInt();
		String sS = next();
		int numK = nextInt();

		char tmp = sS.charAt(numK - 1);

		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < sS.length(); i++) {
			if (sS.charAt(i) != tmp) {
				builder.append('*');
			} else {
				builder.append(tmp);
			}
		}

		out.println(builder.toString());
	}

C問題

  • 解説はインライン
	/*
	 * ●○
	 * この組を作らない
	 *
	 * ○●●●●●●●
	 * これはOK
	 * つまり、
	 * ●の位置を決定したらそれ以降全て●
	 * ●の位置決定前までは○が許可される
	 * ※出現したらではなく、位置決定したらが正確かと思う
	 *
	 * 例えばこんなの
	 * ○●●●○●○●
	 *
	 * 取りうるパターンは以下になる
	 * ●●●●●●●●
	 * ○●●●●●●●
	 * ○○○○○●●●
	 * ○○○○○○○●
	 * ○○○○○○○○
	 *
	 * 例えばこんなの
	 * ●○●●○●○●
	 *
	 * ●●●●●●●●
	 * ○○●●●●●●
	 * ○○○○●●●●
	 * ○○○○○○●●
	 * ○○○○○○○●
	 * ○○○○○○○○
	 *
	 * つまり、
	 * ある位置を決めたら左はすべて白、右はすべて黒になる
	 * 1:左に黒があったら白にする
	 * 2:右に白があったら黒にする
	 * 上記を実装すればよい
	 *
	 */
	private void solveC() {
		int numN = nextInt();
		String sS = next();
		char[] tmp = sS.toCharArray();

		int black = 0;
		int white = 0;
		for (int i = 0; i < tmp.length; i++) {
			if (tmp[i] == '.') {
				white++;
			}
		}
		/*
		 * ○●●●○●○●
		 * 上記の遷移として
		 *
		 * 下記のようにするためには何個置き換える?というカウントをする。
		 * start ○=3  ●=0
		 * i
		 * 0 ○●●●●●●● ○-1     =  ○2個 ●0個置き換え = 合計2個
		 * 1 ○○●●●●●● ○-1 ●+1 =  ○2個 ●1個置き換え = 合計3個
		 * 2 ○○○●●●●● ○-1 ●+2 =  ○2個 ●2個置き換え = 合計4個
		 * 3 ○○○○●●●● ○-1 ●+3 =  ○2個 ●3個置き換え = 合計5個
		 * 4 ○○○○○●●● ○-2 ●+3 =  ○1個 ●3個置き換え = 合計4個
		 * 5 ○○○○○○●● ○-2 ●+4 =  ○1個 ●4個置き換え = 合計5個
		 * 6 ○○○○○○○● ○-3 ●+4 =  ○0個 ●4個置き換え = 合計4個
		 * 7 ○○○○○○○○ ○-3 ●+5 =  ○0個 ●5個置き換え = 合計5個
		 *
		 */
		int res = white;
		for (int i = 0; i < tmp.length; i++) {
			if (tmp[i] == '.') {
				white--;
			} else {
				black++;
			}
			res = Math.min(res, white + black);
		}
		out.print(res);

	}
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?