https://www.acmicpc.net/problem/2532
정렬한다음 중복값 제거하고 최장증가수열 응용했음
풀려고 한지 몇일 됬는데 이거만 계속 생각하고 있을 시간도없고 어렵기도하고 드뎌품
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include <stdio.h> #include <algorithm> #include <vector> using namespace std; typedef struct { int first; int second; } Animal; vector<Animal> animal; int cash[500002] = { 0 }; int ans=0; int cmp(const Animal &a, const Animal &b) { if (a.first != b.first) return a.first < b.first; else return a.second > b.second; } void search(int target, int left, int right) { if (left > right) { if (ans < right+1) { ans = right+1; } if (cash[right + 1] < target) { cash[right + 1] = target; } return; } int mid = (left + right) / 2; if (cash[mid] >= target) { search(target, mid+1, right); } else { search(target, left, mid-1); } return; } int main() { int n; int index=0; scanf("%d", &n); if (n == 0) { printf("%d\n", 0); return 0; } while (n--) { int dum; Animal dummy; scanf(" %d %d %d",&dum, &dummy.first, &dummy.second); animal.push_back(dummy); } sort(animal.begin(), animal.end(),cmp); for (int i = 1; i < animal.size(); i++) { if ((animal[i].first == animal[index].first) && (animal[i].second == animal[index].second)) { continue; } else { index++; animal[index].first = animal[i].first; animal[index].second = animal[i].second; } } cash[0] = 1000000000; for (int i = 0; i <= index; i++) { search(animal[i].second, 0, ans); } printf("%d\n", ans); return 0; } | cs |
다음문제 골라서 옴
stl 에 이진탐색도 있엉
아아 캐시 쓰게 만든거군
cmp 를 Animal 구조체의 멤버 연산자< 함수로 구현하면 더 이쁨
continue를 했으면 else 쓸 필요가..
ㄴ 저도 필요없다는거는 느꼈는데 ㅋㅋ 그냥 제가 읽기 편하려고
연산자오버로딩 말씀하쉬는건가
글고 님이 존재한다는 O(N)은 생각도 안난다는.. 이따가 어제 올린 안내원 코드 함 봐보려고함
ㅇㅇ 연산자 오버로딩으로 구현하면 아래 stl::sort 에서 세번째 인자가 필요없쥬
똑같이 sort 인데 radix 를 쓰면 O(N) 인 것.
cash->cache
문제를 제대로 읽어보니 생각해볼게 더 있긴 한데 그래도 O(N) 은 될듯.
cafe.daum.net/codeinside
LIS는 선형시간에 못풀텐데
ㅋㅋㅋ 나도 이거 접근법 LIS -> 스택 -> LIS 방황하다가 한 3,4시간 걸려 푼듯