ldd1018   4년 전

구조체를 선언한 후에 구조체 전체에 대해 lis를 수행하려 하는데 lower_bound() 시행 부분에서 초기화 관련 오류가 뜨네요.. compare 함수 작성 관련한 문제인 것 같은데 작성방법을 잘 모르겠습니다. 코드에 틀린점 지적해주시면 감사하겠습니다.

rbgo   1년 전

안녕하세요.
저도 비슷한 오류 직면했는데요...

cmp 함수 파라미터가 달라져야 하네요.

upper_bound 함수 call 할때 int 값 넣어줬으면 Cmp 함수에서 기준값을int type으로 넣어줘야 합니다. (첫번째 전달인자를 int로)

Lower_Bound에서는 두번째 값을 int로 주면 될꺼에요.



#include
#include
#include
using namespace std;
int n;
struct st {
int box;
int index;
int h;
int w;
bool operator<(const st& a1)const {
return box < a1.box;
}
}arr[105];
bool comp_0(const st& a1, const st& a2) {
return a1.w < a2.w;
}
bool comp_1(const st& a1, int const& a2) {
// Lower_bound 용 cmp
return a1.w < a2;
}
bool comp_2(int const& a1, const st& a2) {
// upper_bound 용 cmp - 비교 하려고 넣어준 값이 int
return a1 < a2.w;
}
vector gstV;

int main()
{
int ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &arr[i].box, &arr[i].h, &arr[i].w);
arr[i].index = i + 1;
}
sort(arr, arr + n);
for (int i = 0; i < n; i++) printf("%d ", arr[i].box);
for (int i = 0; i < n; i++) {
vector::iterator iter = upper_bound(gstV.begin(), gstV.end(), arr[i].w, comp_2);
if (iter == gstV.end()) {
gstV.push_back(arr[i]);
}
else {
*iter = arr[i];
}
int sum = 0;
for (vector::iterator it = gstV.begin(); it != gstV.end(); ++it) {
sum += (*it).h;
}
if (ans < sum) ans = sum;
}
printf("%d", ans);
return 0;
}

댓글을 작성하려면 로그인해야 합니다.