[JAVA] ABC - 023 - A & B & C.

AtCoder ABC 023 A&B&C AtCoder - 023

Ein Problem

	private void solveA() {
		int x = nextInt();

		int res = 0;
		while (x != 0) {
			res += x % 10;
			x /= 10;
		}

		out.println(res);
	}

B Problem

"Da die Reihenfolge der Kombination von" a "," c "und" b "nicht berücksichtigt wird", handelt es sich tatsächlich um eine "Lügenlösung". .. .. Ein Fix wird bald kommen.

	private void solveB() {
		int numN = nextInt();
		String s = next();

		if ((numN & 1) == 0) {
			out.println(-1);
			return;
		}
		int max = numN / 2;
		int res = 0;
		for (int i = 0; i < max; i++) {
			char down = s.charAt((numN - 1) - (max + 1) - i);
			char up = s.charAt((max + 1) + i);
			if ((down == 'a' && up == 'c') || (down == 'c' && up == 'a') || (down == 'b' && up == 'b')) {
				res = i + 1;
			} else {
				out.println(-1);
				return;
			}
		}

		out.println(res);
	}

C Problem

AtCoder Beginner Contest 023 Erklärung S.12 ~

-Ich schreibe nur eine Beilage über [Feineinstellung \ Untersuchen, wo die Süßigkeiten platziert wurden]

Wenn die Summe gleich K ist

col no\row no 1 2 3
col count\Reihenanzahl 2 1 2
1 1
2 2
3 2

Wenn der Ort, an dem Sie die Süßigkeiten ablegen, $ col count + row count = K $ ist, zählen Sie die Orte, die nicht gezählt werden sollten.

Wenn die Summe gleich K + 1 ist

col no\row no 1 2 3
col count\Reihenanzahl 2 1 2
1 1
2 2
3 2
	private void solveC() {
		int r = nextInt();
		int c = nextInt();
		int k = nextInt();
		int n = nextInt();
		int[] rI = new int[n];
		int[] cI = new int[n];
		int[] rP = new int[r + 1];
		int[] cP = new int[c + 1];
		for (int i = 0; i < n; i++) {
			int tmpR = nextInt() - 1;
			int tmpC = nextInt() - 1;
			rI[i] = tmpR;
			cI[i] = tmpC;
			//Anzahl der Süßigkeiten für jede Zeile von r
			rP[tmpR]++;
			//Anzahl der Süßigkeiten für jede Spalte von c
			cP[tmpC]++;
		}

		/*
		 *Aggregieren Sie die Anzahl der Süßigkeiten für jede Reihe
		 *Wenn diese Nummer für jede Zeile gilt
		 * [1, 2, 2, 0]
		 *Zählen Sie wie folgt
		 * 0=1
		 * 1=1
		 * 2=2
		 * 3=0
		 *Die rPCount-Größe ist N.+1 ist, weil es keine Süßigkeiten mehr gibt
		 */
		int[] rPCount = new int[n + 1];
		for (int i = 0; i < r; i++) {
			rPCount[rP[i]]++;
		}
		//Verarbeiten Sie jede Spalte wie eine Zeile
		int[] cPCount = new int[n + 1];
		for (int i = 0; i < c; i++) {
			cPCount[cP[i]]++;
		}

		long res = 0;
		/*
		 *Berechnen Sie die Kombination grob
		 *Für Eingabebeispiel 1:
		 *Wenn die Anzahl der Süßigkeiten in der Reihe 0 ist, muss die Anzahl der Süßigkeiten in Spalte 3 sein
		 *aus diesem Grund,[Anzahl der Zeilen, in denen r Candy 0 ist]×[Die Anzahl der Zeilen, in denen die Süßigkeit von c 3 beträgt]Berechnung
		 *Wenn die Anzahl der Süßigkeiten in der Reihe 1 beträgt, muss die Anzahl der Süßigkeiten in Spalte 2 betragen.
		 *  [Anzahl der Zeilen, in denen r Süßigkeiten 1 sind]×[Die Anzahl der Zeilen, in denen die Süßigkeit von c 2 beträgt]Berechnung
		 *Dies ergibt eine grobe Kombination
		 */
		for (int i = 0; i <= k; i++) {
			res += rPCount[i] * cPCount[k - i];
		}

		/*
		 *Optimieren
		 *Untersuchen Sie, wo die Süßigkeiten platziert wurden
		 */
		for (int i = 0; i < n; i++) {
			long sum = rP[rI[i]] + cP[cI[i]];
			if (sum == k) {
				/*
				 *Die Tatsache, dass die Summe gleich K ist, zählt die Objekte, die bei der groben Kombinationsberechnung nicht gezählt werden sollten.
				 *Daher wird diese Position von der Zählung ausgeschlossen
				 */
				res--;
			} else if (sum == k + 1) {
				/*
				 *Die Summe ist K.+1 liegt zum Zeitpunkt der groben Kombinationsberechnung außerhalb des Zählbereichs
				 *Daher zählt diese Position
				 */
				res++;
			}
		}
		out.println(res);
	}

C Problem: TLE

--Ist der Speicher niedrig? Es scheint, aber die Geschwindigkeit ist nicht genug. .. ..

	private void solveC3() {
		int r = nextInt();
		int c = nextInt();
		int k = nextInt();
		int n = nextInt();
		//		int[][] masu = new int[r][c];
		Map<Integer, Set<Integer>> wk = new HashMap<Integer, Set<Integer>>();
		int[] rP = new int[r];
		int[] cP = new int[c];
		for (int i = 0; i < n; i++) {
			int tmpR = nextInt() - 1;
			int tmpC = nextInt() - 1;
			//			masu[tmpR][tmpC] = 1;
			Set<Integer> tmpSet = new HashSet<Integer>();
			tmpSet.add(tmpC);
			wk.merge(tmpR, tmpSet, (oldV, newV) -> {
				oldV.addAll(newV);
				return oldV;
			});
			rP[tmpR]++;
			cP[tmpC]++;
		}

		long res = 0;
		Set<Integer> defSet = new HashSet<Integer>();
		for (int i = 0; i < rP.length; i++) {
			for (int j = 0; j < cP.length; j++) {
				int val = wk.getOrDefault(i, defSet).contains(j) ? 1 : 0;
				//				int val = wk.getOrDefault(i, new HashSet<Integer>()).contains(j) ? 1 : 0;
				if (rP[i] + cP[j] - val == k) {
					res++;
				}
			}
		}

		out.println(res);
	}

C Problem: MLE

	/**
	 * MLE
	 */
	private void solveC2() {
		int r = nextInt();
		int c = nextInt();
		int k = nextInt();
		int n = nextInt();
		int[][] masu = new int[r][c];
		int[] rP = new int[r];
		int[] cP = new int[c];
		for (int i = 0; i < n; i++) {
			int tmpR = nextInt();
			int tmpC = nextInt();
			masu[tmpR - 1][tmpC - 1] = 1;
			rP[tmpR - 1]++;
			cP[tmpC - 1]++;
		}

		long res = 0;
		for (int i = 0; i < rP.length; i++) {
			for (int j = 0; j < cP.length; j++) {
				if (rP[i] + cP[j] - masu[i][j] == k) {
					res++;
				}
			}
		}

		out.println(res);
	}

Recommended Posts

ABC - 013-A & B & C.
ABC - 023 - A & B & C.
ABC - 036-A & B & C.
ABC - 010 - A & B & C.
ABC - 028 - A & B & C.
ABC - 015 - A & B & C.
ABC - 128 - A & B & C.
ABC - 012-A & B & C.
ABC - 018 - A & B & C.
ABC - 054 - A & B & C.
ABC - 017 - A & B & C.
ABC - 029 - A & B & C.
ABC - 022 - A & B & C.
ABC - 019 - A & B & C.
ABC - 020 - A & B & C.
ABC - 030 - A & B & C.
ABC - 127 - A & B & C.
ABC - 007 - A & B & C.
ABC - 132 - A & B & C.
ABC - 026 - A & B & C.
ABC - 014 - A & B & C.
ABC - 016 - A & B & C.
ABC - 011-A & B & C.
ABC - 031 - A & B & C.
ABC - 021 - A & B & C.
ABC - 025 - A & B & C.
ABC - 024 - A & B & C.
ABC - 027 - A & B & C.
ABC - 080 - A & B & C.
ABC - 129 - A & B & C & D.
ABC - 133 - A & B & C & D.
ABC - 122 - A & B & C & D.
ABC - 125 - A & B & C & D.
ABC - 130 - A & B & C & D.
ABC - 126 - A & B & C & D.
ABC - 134 - A & B & C & D & E.
ABC - 131 - A & B & C & D & E.
diverta 2019 Programmierwettbewerb A & B & C & D.
AtCoder Anfängerwettbewerb 169 A, B, C mit Rubin
atcoder ABC113 C Problem
ABC093 C - Gleiche Ganzzahlen
atcoder ABC115 C Problem
AtCoder Anfängerwettbewerb 170 A, B, C bis Rubin
Eine Person, die C ++ schreibt, hat versucht, Java zu schreiben
Machen Sie einen SOAP-Aufruf in C #
Rufen Sie C-Sprachfunktionen von Swift aus auf
NLP4J 100 Sprachverarbeitungsklopfen mit NLP4J # 34 "A B"