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

[그리디] 9월 27일 #6

Open
wants to merge 1 commit 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
61 changes: 61 additions & 0 deletions 06_그리디/1213.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

//사전순으로 앞서는 것을 출력
//-> 알파벳 배열의 앞에서부터 순서대로 접근하여 문자열에 넣어주면 된다.
//별도의 정렬이 필요하지 않음
pair<int, int> canNotPalindrome(vector<int>& alpha){
int cnt = 0;
int mid_index=-1;
for(int i = 0; i < 26; i++)
if(alpha[i] % 2 == 1) {
cnt++;
mid_index=i;
alpha[i]--;
}
return {cnt, mid_index};

}


string palindrome(const vector<int>& alpha, int mid_idex){
Copy link

Choose a reason for hiding this comment

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

벡터의 값이 변경되지 않도록 const 붙여주신 점 좋습니다👍

string ans = "";
for(int i = 0; i < 26; i++){
if(alpha[i]>0){
for(int j = 0; j < alpha[i]/2; j++){
ans += i + 'A';
}
}
}
string reverse_ans = ans;
reverse(reverse_ans.begin(), reverse_ans.end());
Copy link

Choose a reason for hiding this comment

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

reverse 함수를 사용해 문자열을 뒤집어주셨네요! 좋아요~

if(mid_idex!= -1){
ans += 'A'+mid_idex;
}
ans+= reverse_ans;
return ans;
}

int main() {
string s;
vector<int> alpha(26, 0);
cin >> s;
for(int i = 0; i < s.size(); i++){
alpha[s[i]-'A']++;
}
pair<int, int> odd_info = canNotPalindrome(alpha);

if(odd_info.first>1) {
cout << "I'm Sorry Hansoo";
return 0;
}
Comment on lines +53 to +56
Copy link

Choose a reason for hiding this comment

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

팰린드롬을 만들 수 없는 조건을 잘 발견해주셨네요!👍


cout<< palindrome(alpha, odd_info.second);

return 0;
}
35 changes: 35 additions & 0 deletions 06_그리디/17451.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

ll minSpeed(int n, vector<ll>& speed){
for(int i=n-2; i>=0; i--){
//현재 속도가 다음 행성까지의 최소 속도보다 느리면
if(speed[i]<speed[i+1]){
//다음 행성까지의 속도 < 현재속도 * n을 만족하는 최소값
//시간 초과 문제 발생 -> 반복문으로 계산하지 않는다.
if(speed[i+1]%speed[i]==0){ //배수관계인 경우
speed[i] = speed[i+1];
}
else{ //배수관계가 아닌 경우
speed[i] = speed[i] * (speed[i+1] / speed[i] + 1);
}

}
Comment on lines +10 to +20
Copy link

Choose a reason for hiding this comment

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

최소 속도를 만드는 조건을 파악해서 문제를 잘 풀어주셨네요!👍

}
return speed[0];
}

int main() {
int n;
cin >> n;
vector<ll> speed(n, 0);
for(int i=0; i<n; i++){
cin >> speed[i];
}

cout << minSpeed(n, speed);
return 0;
}
56 changes: 56 additions & 0 deletions 06_그리디/18111.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <vector>

using namespace std;


pair<int, int> plainGround(int n, int m, int b, vector<vector<int>>& ground, int vmin, int vmax){
int mintime = 256*500*500*2, maxheight = 0;
Copy link
Member

Choose a reason for hiding this comment

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

최댓값을 계산해주신 점 좋습니다! 하지만 더 안전한 방법으로 c++에서 최댓값(INF)을 다루는 여러 가지 방법에 대해 찾아보셔도 좋을 것 같네요!

for(int h=vmin; h<=vmax; h++){ //땅의 높이는 vmin~vmax까지 가능
int add = 0, rm = 0; //더하는 블록의 수, 빼는 블록의 수
for(int i=0; i<n; i++){
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=0; j<m; j++){
int height = ground[i][j] - h;
if(height>=0){ //블록을 제거하는 경우
rm += height;
}
else { //블록을 추가하는 경우
add += -height;
}
}
}
//더하는 블록의 총 개수, 빼는 블록의 총 개수를 가지고 시간 계산
if(rm+b-add>=0){//사용하는 총 블록의 수가 음수가 되면 안됨
int time = add + rm * 2;
if(mintime >= time){
//최소시간 갱신되는 경우 (동일한 최소시간인 경우 최대높이인 답을 반환하도록 조건에 '등호' 사용)
mintime = time;
maxheight = h;
}

}
}

return {mintime, maxheight};
}

int main() {
int n, m, b, input;
int vmin = 257, vmax = 0;
cin >> n >> m >> b;
vector<vector<int>> ground (n, vector<int>(m, 0));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin >> input;
//높이 중 최소와 최대를 구함
vmin = min(vmin, input);
vmax = max(vmax, input);
ground[i][j] = input;

}
}
pair<int, int> ans = plainGround(n, m, b, ground, vmin, vmax);
Copy link
Member

Choose a reason for hiding this comment

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

많이 쓰이는 자료형은 typedef를 이용해서 별칭 지정해보시는 건 어떨까요?

cout << ans.first << " " << ans.second;

return 0;
}