Skip to content
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

12-yuyu0830 #49

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
| 5์ฐจ์‹œ | 2024.03.29 | ํƒ์ƒ‰ | [์Šค๋„์ฟ ](https://www.acmicpc.net/problem/2239) | - |
| 6์ฐจ์‹œ | 2024.04.03 | ์ž๋ฃŒ๊ตฌ์กฐ | [๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์• ](https://www.acmicpc.net/problem/14621) | - |
| 7์ฐจ์‹œ | 2024.04.04 | ์ €์šธ | [์ €์šธ](https://www.acmicpc.net/problem/2437) | - |
| 8์ฐจ์‹œ | 2024.04.11 | ๊ทธ๋ฆฌ๋”” | [๋ณด์„๋„๋‘‘](https://www.acmicpc.net/problem/1202) | - |
| 9์ฐจ์‹œ | 2024.04.12 | ์ •๋ ฌ | [์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—… - ๋ฒ„๋ธ” ์ •๋ ฌ 3](https://www.acmicpc.net/problem/23970) | - |
| 10์ฐจ์‹œ| 2024.04.29 | ๋ฐฑํŠธ๋ž˜ํ‚น | [๋น„์ˆ](https://www.acmicpc.net/problem/1799) | - |
| 11์ฐจ์‹œ| 2024.05.08 | ๊ทธ๋ฆฌ๋”” | [์—ฐ๋ฃŒ ์ฑ„์šฐ๊ธฐ](https://www.acmicpc.net/problem/1826) | - |
| 12์ฐจ์‹œ| 2024.05.13 | ํ•ด์‹œ | [๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ](https://www.acmicpc.net/problem/2143) | - |
---
54 changes: 54 additions & 0 deletions yuyu0830/ํ•ด์‹œ/2143.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

unordered_map<int, long long int> m[2];
vector<int> v[2];

int main() {
int n; cin >> n;

for (int i = 0; i < 2; i++) {
int t; cin >> t;
int sum = 0;

// initialize value
v[i].push_back(0);

// save prefix sum
while (t--) {
int a; cin >> a;
sum += a;
v[i].push_back(sum);
}
}

for (int i = 0; i < 2; i++) {
int len = v[i].size();

// save all case of sub set
for (int x = 0; x < len; x++) {
for (int y = x + 1; y < len; y++) {
int tmp = v[i][y] - v[i][x];

// if exist
if (m[i].find(tmp)!= m[i].end())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฐฉ์‹์€ ๋ฌธ์ œ๋ฅผ ์œ ๋ฐœํ• ์ˆ˜๋„ ์žˆ๊ธด ํ•œ๋ฐ ์ €๋Š” ๊ฐ„ํ˜น ์ด๋ ‡๊ฒŒ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์„œ ๋ง์”€๋“œ๋ฆฝ๋‹ˆ๋‹ค.

C๋ฅผ ๋น„๋กฏํ•œ ํ™•์žฅ ์–ธ์–ด๋“ค์€ default ์ƒ์„ฑ์ž๋“ค์„ ๋”ฐ๋กœ ์„ ์–ธํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋Š” ์ƒ์„ฑ์ž ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์„ ์–ธํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ๋ฐฐ์—ด์—์„œ๋„ 0์œผ๋กœ ์ž๋™์œผ๋กœ ์ฑ„์›Œ์ง€๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ.

map์—์„œ๋„ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น key๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์–˜๋„ค๋“ค์€ if๋ฌธ์—์„œ ์—†๋Š” key์˜ ๊ฐ’์— ๋Œ€ํ•ด value๋Š” 0์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰ ์กด์žฌํ•˜์ง€ ์•Š๋Š” key๋Š” ์กฐ๊ฑด๋ฌธ์—์„œ๋Š” 0์œผ๋กœ bool ํ˜•์ธ false๋กœ ์ธ์‹ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. {(false == 0)์ด๊ธฐ ๋•Œ๋ฌธ์—}
๊ทธ๋ž˜์„œ ์ €๋Š” ๊ทธ๋ƒฅ m[i][key]++; ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์„œ ์ข‹์€ ์ฝ”๋“œ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋‹จ์ˆœํ•˜๊ฒŒ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•ด์„œ ์”๋‹ˆ๋‹ค.

C์—์„œ find๋Š” end๋ฅผ ๋งž์ดํ•  ๋•Œ ๊นŒ์ง€ ๋ชจ๋“  ์š”์†Œ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2์ค‘ 3์ค‘ for๋ฌธ์— find ์กฐ๊ฑด๋ฌธ์ด ๋ถ™๋Š”๋‹ค๋ฉด ์ง€์ˆ˜์Šน์˜ ๋ณต์žก๋„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unordered_map์€ Hash ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— find ํ•จ์ˆ˜๋Š” $O(1)$ ์‹œ๊ฐ„์— ๊ฑฐ์˜ ๊ทผ์ ‘ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  [] operator๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒŒ ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ๋™์ž‘์„ ์œ ๋ฐœํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋‹จ์ˆœํžˆ find()๋‚˜ count()๋ฅผ ํ†ตํ•ด ์›์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š” ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฑฐ๋ž‘์€ ๋‹ค๋ฅด๊ฒŒ, [] operator๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด default value์— ๋Œ€ํ•œ ๋™์ž‘์ด ์ •์˜๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

m[i][tmp] = m[i][tmp] + 1;

else
m[i].insert(make_pair(tmp, 1));

}
}
}

long long int ans = 0;

// count case that can make 'T'
for (auto p : m[1])
if (m[0].find(n - p.first) != m[0].end()) ans += m[0][n - p.first] * p.second;

printf("%lld\n", ans);
}