Skip to content

[박예진-15주차 알고리즘 스터디] #91

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions 박예진/15주차/[백준 10986] 나머지 합.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <map>
using namespace std;

int arr[1000001];
long long preSum[1000001];
int mod[1000001];

// nC2
long long combi(long long n) {
return n * (n - 1) / 2;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

int N, M;
cin >> N >> M;

preSum[0] = 0;
mod[0] = 0;

for (int i = 1; i <= N; i++) {
cin >> arr[i];
preSum[i] = preSum[i - 1] + arr[i];
mod[i] = preSum[i] % M;
if (mod[i] < 0) mod[i] += M;
}

map<int, long long> m; // 공통된 나머지 개수 세기
for (int i = 0; i <= N; i++) {
m[mod[i]]++;
}

long long res = 0;
for (auto iter : m) {
res += combi(iter.second);
}

cout << res;

return 0;
}
29 changes: 29 additions & 0 deletions 박예진/15주차/[백준 13164] 행복 유치원.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int N, K;
int arr[300001];

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

int sum = 0;
cin >> N >> K;
vector<int> v;
cin >> arr[0];
for (int i = 1; i < N; i++) {
cin >> arr[i];
v.push_back(arr[i] - arr[i - 1]);
}
sort(v.begin(), v.end());

for (int i = 0; i < N - K; i++) {
sum += v[i];
}
cout << sum;

return 0;
}
26 changes: 26 additions & 0 deletions 박예진/15주차/[백준 1699] 제곱수의 합.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;

int N;
int dp[100001];

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

cin >> N;
for (int i = 1; i <= N; i++) {
dp[i] = i;
for (int j = 1; j * j <= i; j++) {
dp[i] = min(dp[i], dp[i - j * j] + 1);
}
}

cout << dp[N];

return 0;
}
59 changes: 59 additions & 0 deletions 박예진/15주차/[백준 1918] 후위 표기식.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

string str, res = "";
cin >> str;

stack <char> s;
for (int i = 0; i < str.size(); i++) {
if ('A' <= str[i] && str[i] <= 'Z') {
res += str[i];
}
else {
switch (str[i]) {
case '(':
s.push(str[i]);
break;
case '*':
case '/':
while (!s.empty() && (s.top() == '*' || s.top() == '/')) {
res += s.top();
s.pop();
}
s.push(str[i]);
break;
case '+':
case '-':
while (!s.empty() && s.top() != '(') {
res += s.top();
s.pop();
}
s.push(str[i]);
break;
case ')':
while (!s.empty() && s.top() != '(') {
res += s.top();
s.pop();
}
s.pop();
break;
}
}
}
while (!s.empty()) {
res += s.top();
s.pop();
}

cout << res;

return 0;
}