Skip to content
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
34 changes: 34 additions & 0 deletions 9월 20일 - 우선순위 큐/11286.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <queue>

using namespace std;

struct cmp{
bool operator()(int &a, int &b){
if(abs(a) != abs(b))
return abs(a) > abs(b);
return a > b;
}
};
Comment on lines +6 to +12
Copy link
Member

Choose a reason for hiding this comment

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

👍


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

priority_queue<int, vector<int>, cmp> pq;
for(int i=0; i<n; i++){
int t;
cin >> t;
if(t == 0){
if(!pq.empty()) {
cout << pq.top() << '\n';
pq.pop();
}
else
cout << '0' << '\n';
}
else
pq.push(t);
}
}

51 changes: 51 additions & 0 deletions 9월 20일 - 우선순위 큐/11723.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#include <iostream>
#include <vector>

using namespace std;

int main() {

ios::sync_with_stdio(false);
cin.tie(NULL);

vector<bool> s(21, false);
Comment on lines +11 to +12
Copy link
Member

Choose a reason for hiding this comment

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

잘 푸셨습니다! 근데 이 배열조차 사용하지 않고 int 변수 하나로 문제를 풀 수 있는 방법도 있어요!
비트마스킹에 대해 알아보시겠어요? 좀 어려울 수도 있으니 알아만 보셔도 좋아요


int n;
cin >> n;

string str;

for (int i = 0; i < n; i++) {
cin >> str;
int x;
if (str == "all") {
for (int i = 1; i < 21; i++) {
s[i] = true;
}
} else if (str == "empty") {
for (int i = 1; i < 21; i++) {
s[i] = false;
}
} else if (str == "add") {
cin >> x;
if (!s[x])
s[x] = true;
} else if (str == "remove") {
cin >> x;
if (s[x])
s[x] = false;
} else if (str == "check") {
cin >> x;
if (s[x])
cout << '1' << '\n';
else
cout << '0' << '\n';
Comment on lines +40 to +43
Copy link
Member

Choose a reason for hiding this comment

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

이것도 아래 toggle 처럼 한 출로 나타낼 수 있어요!

} else {
cin >> x;
s[x] = !s[x];
}
}
}


49 changes: 49 additions & 0 deletions 9월 20일 - 우선순위 큐/12018.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#include <iostream>
#include <queue>

using namespace std;

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

priority_queue<int, vector<int>, greater<>> least;
priority_queue<int> temp;

for(int i=0; i<n; i++){
int people, limit;
cin >> people >> limit;
//신청한 사람이 수강인원보다 작다면
if(people < limit){
least.push(1);
for(int j=0; j<people; j++){
int t;
cin >> t;
}
Comment on lines +20 to +23
Copy link
Member

Choose a reason for hiding this comment

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

이 부분은 중복되니까 밖으로 빼면 어떨까요?

}
else{
temp = priority_queue<int>();
for(int j=0; j<people; j++){
int t;
cin >> t;
temp.push(t);
}
//최소로 배당해야할 마일리지 찾고 입력
for(int j=0; j<limit-1; j++){
temp.pop();
}
least.push(temp.top());
}
}

int answer=0, total=0;
while(total <= m && !least.empty()){
total += least.top();
least.pop();
if(total <= m)
answer ++;
}

cout << answer;
}
35 changes: 35 additions & 0 deletions 9월 20일 - 우선순위 큐/15903.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <queue>

using namespace std;

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

priority_queue<long long, vector<long long>, greater<>> q;

for(int i=0; i<n; i++){
int t;
cin >> t;
q.push(t);
}

for(int i=0; i<m; i++){
long long a, b;
a = q.top();
q.pop();
b = q.top();
q.pop();
q.push(a+b);
q.push(a+b);
Comment on lines +19 to +25
Copy link
Member

Choose a reason for hiding this comment

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

여긴 중복이 조금 있으니까 반복문으로 처리해도 좋아요!

}

long long answer;
for(int i=0; i<n; i++){
answer += q.top();
q.pop();
}

cout << answer;
}
77 changes: 77 additions & 0 deletions 9월 20일 - 우선순위 큐/3613.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

#include <iostream>

using namespace std;

string from_cpp(int i, string answer, string str){
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

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

카멜 표기법 사용해주세요~

for(int j=i; j<str.length(); j++){
char c = str[j];
//c++인데 대문자 있으면 에러
if('A' <= c && c <= 'Z')
return "Error!";
//_이 연속을 나오면 에러
if(c == '_'){
if(str[j-1] == '_')
return "Error!";
}
else{
//소문자라면 이전 문자가 _이면 대문자 입력
if(str[j-1] == '_')
answer += toupper(c);
else
answer += c;
}
}

return answer;
}

string from_java(int i, string answer, string str){
for(int j=i; j<str.length(); j++){
char c = str[j];
//자바인데 _ 입력받으면 에러
if(c == '_')
return "Error!";
//대문자이면 _와 소문자 입력
if('A' <= c && c <= 'Z'){
answer += '_';
answer += tolower(c);
}
else
answer += c;
Comment on lines +38 to +41
Copy link
Member

Choose a reason for hiding this comment

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

이 부분은 합칠 수 있는 방법이 있어요~

}
return answer;
}

int main(){
string str;
cin >> str;

string answer = "";
//첫글자가 _, 대문자이거나 마지막이 _이면 에러
if(str[0] == '_' || ('A' <= str[0] && str[0] <= 'Z') || str[str.length()-1] == '_'){
cout << "Error!";
return 0;
}
else{
Comment on lines +55 to +56
Copy link
Member

Choose a reason for hiding this comment

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

위에 있는 if 문에 걸리면 무조건 리턴하니 이 else는 필요 없어요

answer += str[0];
}

for(int i=1; i<str.length(); i++){
char c = str[i];
if('a' <= c && c <= 'z'){
answer += c;
}
if(c == '_') { //_을 입력받으면 c++
answer = from_cpp(i, answer, str);
break;
}
if('A' <= c && c <= 'Z') { //대문자 입력받으면 java
answer = from_java(i, answer, str);
break;
}
}

cout << answer;
}