LoginSignup
4
2

More than 1 year has passed since last update.

[ネタばれ] 長テーブルのうなぎ屋 (paizaランク B 相当)

Last updated at Posted at 2018-04-16

境界線の処理をどうするかの問題だよなこれ

import java.io.InputStream;
import java.util.Scanner;

class UnagiNew {

	public static void main(String[] args) {
		System.out.println(exec(System.in));
	}

	public static int exec(InputStream is) {
		try (Scanner sc = new Scanner(is);) {
			// 座席数
			int n = sc.nextInt();
			boolean[] bools = new boolean[n];
			// グループ数
			int m = sc.nextInt();
			for (int i = 0; i < m; i++) {
				// 人数
				int a = sc.nextInt();
				// 開始位置
				int b = sc.nextInt();
				// チェック
				boolean check = false;
				for (int j = b; j < a + b; j++)
					if (bools[j % bools.length] && (check = true))
						break;
				if (check)
					continue;
				for (int j = b; j < a + b; j++)
					bools[j % bools.length] = true;
			}
			int ret = 0;
			for (boolean b : bools)
				if (b)
					ret++;
			return ret;
		}
	}
}

関連:
https://qiita.com/krppppp/items/a75205c9dcb4165ca2b4

最新の技術を利用し、可読性無視で記載を修正した場合

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;


public class App {
    public static void main(String[] args) throws Exception {
        try (Scanner sc = new Scanner(System.in);) {
            int[] c = new int[sc.nextInt()];// 座席数
            IntStream.range(0, sc.nextInt() /* 入力数 */).forEach(i->{
                int a = sc.nextInt(),b = sc.nextInt(); // a=人数, b=開始位置
                if (IntStream.range(b, a+b).anyMatch(j->c[j % c.length]==1)) return;
                IntStream.range(b, a+b).forEach(j->c[j % c.length]=1);
            });
            System.out.println(Arrays.stream(c).filter(v->v==1).count());
        }
    }
}

stream に置換するとここまで短くなるがもっとショートに成る案を募集してみる。

4
2
2

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
4
2