-
Notifications
You must be signed in to change notification settings - Fork 1
31-kangrae-jo #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
31-kangrae-jo #122
Conversation
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋งค ๋ฒ ๋ถ๋ถ ๋ฐฐ์ด ํฉ์ ๊ตฌํ๋ ๊ฑด ๋ฌด์กฐ๊ฑด ์๊ฐ์ด๊ณผ ๋๋๊น ๋ฏธ๋ฆฌ ์บ์ฑ์ ํด๋์๊ฒ ๋ค -> ๋ถ๋ถํฉ
์ด ๋ถ๋ถ์ ์บ์นํ๋๋ฐ ๊ทธ ๋ค์์ ์ด์ผ ํธ๋
ธ.... ์ด๋ฌ๊ณ ์์๋๋ฐ 'ํฉ์ด 0์ธ ๋ค ์ ์' ๋ฌธ์ ์ ๋น์ทํ๋ค๋ ์ฝ๋ฉํธ๋ฅผ ๋ณด๊ณ ๊ฐ์ ์ก์๋ค์.
์ฝ๊ฐ ์ด๊ฑฐ์ง?๋ผ๊ณ ๋ณด์ผ ์๋ ์๋๋ฐ, ํฌํฌ์ธํฐ๋ ์ด์ง ํ์ ์์ฐ๊ณ ๋จ์ํ ํ ์ชฝ ๋ถ๋ถํฉ ๋ฐฐ์ด์ ํตํด ๋ง๋ค ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ map์ ๋ค ๋๋ ค ๋ฐ์ผ๋ฉด ๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋๋จธ์ง ํ ์ชฝ ๋ถ๋ถํฉ ๋ฐฐ์ด์ ํตํด ๋ง๋ค ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ํ์ธํ๋ฉด์, T - (๊ฒฝ์ฐ์ ์)๊ฐ map์ ์กด์ฌํ๋ ์ง ์ฒดํฌํด์ ๊ทธ ๊ฐ์๋งํผ ๋ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int64 T; cin >> T;
int n; cin >> n;
vector<int64> A(n), prefixA(n + 1, 0);
for(int i = 0; i < n; ++i) {
cin >> A[i];
prefixA[i + 1] = prefixA[i] + A[i];
}
int m; cin >> m;
vector<int64> B(m), prefixB(m + 1, 0);
for(int i = 0; i < m; ++i) {
cin >> B[i];
prefixB[i + 1] = prefixB[i] + B[i];
}
unordered_map<int, int64> numSubset;
numSubset.reserve(n * n);
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j <= n; ++j) {
++numSubset[prefixA[j] - prefixA[i]];
}
}
int64 ans = 0;
for(int i = 0; i < m; ++i) {
for(int j = i + 1; j <= m; ++j) {
int64 val = prefixB[j] - prefixB[i];
if(numSubset.count(T - val)) {
ans += numSubset.at(T - val);
}
}
}
cout << ans;
return 0;
}๋ค๋ง ์๋ฃํ ๋๋ฌธ์ 4ํธ ๊ทธ๋ฅ ๋ ๋ ค๋จน์๊ฑด ์๋น๋ฐ... ใ
kokeunho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ๋ฐฐ์ด a, b ๊ฐ๊ฐ ์ด์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๋์ ํฉ์ ์ ์ฅํด๋๊ณ
์ด์ฐจ์ ๋ฐฐ์ด์ ํ์ํ๋ฉด์ ํฉ์ด T๊ฐ ๋๋ ์กฐํฉ์ ์ฐพ์ผ๋ ค ํ์ต๋๋ค.
๊ทผ๋ฐ ๊ทธ๋ ๊ฒ ๋๋ฉด ์๊ฐ ๋ณต์ก๋๊ฐ ์ต์
์ ๊ฒฝ์ฐ O(n^2 * m^2)๊ฐ ๋ผ์ ํ๋ฆฐ ํ์ด์์ต๋๋ค.
์ข ๊ณ ๋ฏผํด๋ณด๋ค ํ์ด๋ฅผ ์ฐธ๊ณ ํ๋๋ฐ
์๊ฐ๋ณด๋ค ํจ์ฌ ๊ฐ๋จํ ํ์ด์์ต๋๋ค.
์ฐ์ ๊ฐ๋๋๊ณผ ๋์ผํ๊ฒ ๋ฐฐ์ด a, b์ ๋ฒ์๋ณ ๋์ฌ ์ ์๋ ๋์ ํฉ์ ๋ชจ๋ ์ ์ฅํฉ๋๋ค (sumA, sumB).
๊ทธ ๋ค์๋ถํฐ๋ ์ ๋ Map์ ์ฌ์ฉํ์์ต๋๋ค.
sumB๋ฅผ Map ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํ์ฌ ๊ฐ ๋์ ํฉ ๊ฐ๋ง๋ค ๋์ผํ ๊ฐ์ด ๋ช ๊ฐ๊ฐ ๋์ค๋์ง ์ ๋ฆฌํฉ๋๋ค.
->๋์ ํฉ์ ๊ฐ์ key๋ก ๋์ผํ ๊ฐ์ ๊ฐ์๋ฅผ value๋ก Map์ ์ ์ฅํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ sumA๋ฅผ ์ํํ๋ฉฐ sumB์์ T-sumA_i์ ํด๋นํ๋ ๊ฐ๋ค์
์ต์ข
๋ต์ ๋ณ์์ ๋ํด์ฃผ์์ต๋๋ค.
์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค. ์๊ณ ํ์ จ์ต๋๋ค!
Details
import java.util.*;
public class Main {
static int T, n, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
n = sc.nextInt();
int[] arrA = new int[n];
for (int i = 0; i < n; i++) {
arrA[i] = sc.nextInt();
}
m = sc.nextInt();
int[] arrB = new int[m];
for (int i = 0; i < m; i++) {
arrB[i] = sc.nextInt();
}
List<Integer> sumA = findAllSums(arrA);
List<Integer> sumB = findAllSums(arrB);
Map<Integer, Integer> countB = new HashMap<>();
for (int sum : sumB) {
countB.put(sum, countB.getOrDefault(sum, 0) + 1);
}
long answer = 0;
for (int sum : sumA) {
answer += countB.getOrDefault(T-sum, 0);
}
System.out.print(answer);
}
static List<Integer> findAllSums(int[] arr) {
List<Integer> response = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
int sum = 0;
for (int j = i; j < arr.length; j++) {
sum += arr[j];
response.add(sum);
}
}
return response;
}
}
g0rnn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋์ ํฉ + ํฌํฌ์ธํฐ, ๋์ ํฉ + ์ด์งํ์์ผ๋ก ํ์ด์ผํ๋ค.! ๋ ๋๊ปด์ก์ง๋ง ์ง์ ์ ์ธ ์๋ฃจ์
์ด ์๊ฐ๋์ง ์์์ต๋๋ค. ์ ์ด์ ๋์ ํฉ์ 1~i๊น์ง์ ํฉ๋ง ์๊ฐํ๊ณ ์์์ต๋๋ค. ์ด์ฐจํผ ๋ด๊ฐ ์ํ๋ ๊ตฌ๊ฐ์ ํฉ์ ๋์ ํฉ์ index ์ ๊ทผ์ผ๋ก ์์ ์๊ฐ ๋ด์ ๊ตฌํ ์ ์์ผ๋ ๋ฌธ์ ์๋ค๊ณ ์๊ฐํด์ต๋๋ค. ๋ชจ๋ ๋ถ๋ถํฉ์ ๊ตฌํ ์๊ฐ์ ๋ชปํ๋๊ฑฐ์ฃ ..
๊ทธ๋ ๊ฒ ์๋ฃจ์ ์ ์๊ฐํ๋ค๊ฐ ์ฌ๊ทํจ์๋ก ์๋์ ๋ฒ์๋ฅผ ์ค์ฌ๊ฐ์! ์ด๋ฐ ์๊ฐ๊น์ง ํ์ง๋ง.. ๋ฌธ์ ๋ฅผ ๋ณด๋ ๋ ๋ฐฐ์ด์ ๋ถ๋ถํฉ์ ๋ชจ๋ ์ฌ์ฉํ๋ผ๊ณ ๋์ด์์ด.. ๊ทธ๋ฅ ๋ต์ ๋ดค์ต๋๋ค.
๊ฐ๋๋์ ์ค๋ช ์ ๋ฃ๊ณ ๋ ์ดํด๋์ง ์์ ์ฝ๋๋ก ์ดํดํ์ต๋๋ค. ๋ถ๋ถํฉ ๊ตฌํ๊ธฐ + ์ ๋ ฌ + ๋ฒ์ ์ค์ด๊ธฐ ์ด๋ ๊ฒ ํฐ ๋ก์ง์ผ๋ก ๋ณด๋ ์๋ฃจ์ ์ด ๋จ๋ฒ์ ์๊ฐ๋๋๊ตฐ์
(while๋ก ์ค๋ณต๋๋ ๋ถ๋ถํฉ์ ์ฒ๋ฆฌํด์ผ ํ๋ค๋ ์ฌ์ค์ ์ค์ ๋ฌธ์ ํ์ด ๋ ์ ๋ ์๊ฐํด๋ด์ง ๋ชปํ์๊ฑฐ ๊ฐ๋ค์..ํํ)
๊ณ ์ํ์ จ์ต๋๋ค~
package beakjoon;
import java.util.*;
import java.io.*;
public class Sol2143 {
static int T, n, m;
static int[] a, b;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(br.readLine());
n = Integer.parseInt(br.readLine());
a = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
m = Integer.parseInt(br.readLine());
b = new int[m];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < m; i++) {
b[i] = Integer.parseInt(st.nextToken());
}
br.close();
List<Integer> partialA = allPartialSum(a, n);
List<Integer> partialB = allPartialSum(b, m);
partialA.sort(Integer::compareTo);
partialB.sort(Integer::compareTo);
long answer = 0;
int left = 0, right = partialB.size() - 1;
while (left < partialA.size() && 0 <= right) {
int sum = partialA.get(left) + partialB.get(right);
if (sum == T) {
long cntA = 1, cntB = 1;
while (left < partialA.size() - 1 && partialA.get(left).equals(partialA.get(left + 1))) {
cntA++;
left++;
}
while (0 < right && partialB.get(right).equals(partialB.get(right - 1))) {
cntB++;
right--;
}
answer += cntA * cntB;
left++;
right--;
} else if(sum < T) {
left++;
} else {
right--;
}
}
System.out.println(answer);
}
private static List<Integer> allPartialSum(int[] arr, int size) {
List<Integer> tmp = new ArrayList<>();
for (int i = 0; i < size; i++) {
int sum = 0;
for (int j = i; j < size; j++) {
sum += arr[j];
tmp.add(sum);
}
}
return tmp;
}
}
wnsmir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์์ ๋๋ฌธ์ ์ธ๊ทธ๋จผํธํธ๋ฆฌ๋ฅผ ํ๊ณ ์ด๋ฌธ์ ๋ฅผ ๋ณด๋
๋ญ๊ฐ ๋ ์ธ๊ทธ๋จผํธํธ๋ฆฌ๋ฌธ์ ์ธ๊ฐ? ํ๊ณ ์๊ฐํ์ต๋๋ค ใ
ใ
ใ
ใ
๊ฒฐ๊ตญ ๋ชจ๋ ํฉ์ ๊ตฌํด์ผํ๊ธฐ ๋๋ฌธ์ DP์ฒ๋ผ ๋์ ํฉ์ผ๋ก ๋ฏธ๋ฆฌ ๊ณ์ฐํด๋๋ ๋ฐฉ์์ ํ ์ ์์์ต๋๋ค.
์ฌ๊ธฐ๊น์ง๋ ๊ฐ๋๋๊ณผ ๊ฐ์๋๋ฐ
์ ๋ T-a๋ฅผ ํฌํฌ์ธํฐ๊ฐ ์๋ ์นด์ดํฐ๋ก ์ฐพ์์ต๋๋ค.
from collections import Counter
c = Counter([1,2,2,3,3,3])
print(c) # Counter({3: 3, 2: 2, 1: 1})์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
from collections import Counter
T = int(input())
n = int(input())
A = list(map(int, input().split()))
m = int(input())
B = list(map(int, input().split()))
# ๋์ ํฉ ๊ตฌํ๊ธฐ
def prefix_sums(arr):
prefix = [0]
for x in arr:
prefix.append(prefix[-1] + x)
return prefix
prefixA = prefix_sums(A)
prefixB = prefix_sums(B)
# ๋ถ ๋ฐฐ์ด ํฉ ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ
def subarray_sums(prefix):
sums = []
n = len(prefix)-1
for i in range(1, n+1):
for j in range(i, n+1):
sums.append(prefix[j] - prefix[i-1])
return sums
subA = subarray_sums(prefixA)
subB = subarray_sums(prefixB)
# subB ๋น๋ ์ธ๊ธฐ
countB = Counter(subB)
# ์ ๋ต ๊ตฌํ๊ธฐ
answer = 0
for a in subA:
answer += countB[T - a]
print(answer)
๐ ๋ฌธ์ ๋งํฌ
[๋ ๋ฐฐ์ด์ ํฉ]
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
์ด๋ฒ์ฃผ ํ ์์ผ์ ๋ชป์ฌ๋ฆด๊ฒ ๊ฐ์์ ๋ฏธ๋ฆฌ ์ฌ๋ฆฝ๋๋ค...
์ค๋ ๋ฌธ์ ๋ ๋์ ํฉ ๊ด๋ จ ๋ฌธ์ ์ ๋๋ค.
์ด ๋ฌธ์ ๋ ๋ฐฉ๋ฒ์ ์๋ฉด ์ํ! ํ๊ณ ๋ฒ๋ฉ์ด์ค๊ฒ๋๋ค.
์ ๋ ์ฒ์์ 2์ฐจ์ ๋ฐฐ์ด์ DP์ฒ๋ผ ๋ชจ๋ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ์ ๋ํด๋์ผ๋ ค๊ณ ํ๋๋ฐ์.. ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์ ์ฅ์ ์ด๋ป๊ฒ๋ ํ๋คํด๋, ๋์ค์ ๊ทธ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ๋ค์ ์กฐํฉํ๊ธฐ๊ฐ ์ฝ์ง ์๋ค๋ ๊ฒ์ด์์.
๊ทธ๋์ ์ฐพ์๋ธ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์์.
์๋ง ๋ฌธ์ ๋ฅผ ์ข ์ค๋ ๊ณ ๋ฏผํด๋ณด์ จ๋ค๋ฉด ์์ ๋ก์ง์ ์ฝ๊ฒ ์ดํดํ ์ ์์ ๊ฑฐ์์.
์ด ๋ฌธ์ ํธ์ ๋ถ๋ค์ [ํฉ์ด 0์ธ ๋ค ์ ์]์ด๊ฒ๋ ํ์ด๋ณด์ ์ ๋ฌดํ์ผ๋ก ์ฆ๊ธฐ์๊ธธ ๋ฐ๋๋๋ค.
์ ์ฒ๋ผ ๋ฐฉ์์ ๊น๋จน์ง ์๋๋ก ๊ธฐ๋ก๋ ์ ํ์๊ธธ ๋ฐ๋๋๋ค.
์ ๊ทธ๋ฆฌ๊ณ ์ฌ์ฉํ๋ ์ธ์ด์
upper_bound,lower_boundํจ์๊ฐ ์๋ ์ฌ๋๋ค์ ์๋์ ๊ฐ์ ํ์ด๋ ๊ฐ๋ฅํ๋ ์ฐธ๊ณ ํ์๋ฉด ๋ฉ๋๋ค.๋ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ์ด T ์ผ๋ while๋ฌธ์ผ๋ก ์ค๋ณต์ ์ฒดํฌํด์ฃผ๋ ๋ถ๋ถ๊ณผ left++ , right-- ํ๋ ๋ก์ง์ ์ต์ํ ํ ์ ์์ด์.
๋งค๋ฒ ์ด๋ถ ํ์์ ํ๊ธฐ ๋๋ฌธ์ ์ ๊ฐ ํ์ผ๋ก ์ฌ๋ ค๋์ ์ฝ๋(ํฌํฌ์ธํฐ)๋ณด๋ค๋ ๋๋ฆฝ๋๋ค.
์ด๋ถ ํ์์ subB๋ง ์ ๋ ฌ ํ๋ฉด ๋ผ์ ์ข ๋ ๋น ๋ฅด์ง๋ง, ๋ฉ์ธ ๋ก์ง์์ O(Nยฒ log M) ์ ๋๋ค.
ํฌ ํฌ์ธํฐ๋ O(Nยฒ + Mยฒ) ์ด์ฃ .
์ด๋ถํ์์ ์ฝ๋๊ฐ ์๋ ๊ฐ๊ฒฐํ๋ ์ถฉ๋ถํ ์ฅ์ ์ด ์๋๊ฒ ๊ฐ์์.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
2์ฐจ์ ๋ฐฐ์ด์ ๋ถ๋ถํฉ์ ๊ตฌํด๋๋๋ค๋ ๊ณ ์ ๊ด๋ ๊บ ๊ธฐ
์์ ์ ํ์๋ ๋ฌธ์ ๊ผญ๊ผญ์น์ด๋จน๊ธฐ