Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用堆来做很非主流吗?看了一下全是线段树。 维护一个堆,在每张海报开始和结束的地方查看顶部是哪张海报标记下来 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 10005 #define BEGIN 0 #define DUMMY 1 #define END 2 struct Event { int pos, order, eventType; }; int heap[MAXN], heapEnd; int Compare(const void *a, const void *b) { Event *eventA = (Event*)a, *eventB = (Event*)b; // Order by pos first in inc order if (eventA->pos != eventB->pos) return eventA->pos - eventB->pos; else { // Then order by event type BEGIN first and then END if (eventA->eventType != eventB->eventType) return eventA->eventType - eventB->eventType; // Then order by order in desc order for BEGIN; or inc order for END if (eventA->eventType == BEGIN) return eventB->order - eventA->order; else return eventA->order - eventB->order; } } void Push(int order) { heap[++heapEnd] = order; int i = heapEnd; while (i > 1) { if (heap[i >> 1] > heap[i]) return; int tmp = heap[i >> 1]; heap[i >> 1] = heap[i]; heap[i] = tmp; i >>= 1; } } void Pop() { heap[1] = heap[heapEnd--]; int i = 1; while ((i << 1) <= heapEnd) { int l = i; if (heap[i << 1] > heap[l]) l = (i << 1); if (heap[(i << 1) + 1] > heap[l]) l = (i << 1) + 1; if (l == i) return; int tmp = heap[l]; heap[l] = heap[i]; heap[i] = tmp; i = l; } } int main() { int i, caseCount, n, l, r, ans; char visible[MAXN], offWall[MAXN]; Event events[MAXN * 3]; scanf("%d", &caseCount); while (caseCount--) { memset(visible, 0, sizeof(visible)); memset(offWall, 0, sizeof(offWall)); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d %d", &l, &r); int idx = i * 3; events[idx].pos = l, events[idx].order = i, events[idx].eventType = BEGIN; idx++; events[idx].pos = r, events[idx].order = i, events[idx].eventType = END; // Need to check what's on top at r + 1 // Consider the case: /* 3 1 5 1 2 4 5 */ // 0 1 2 3 4 // - - - - - // - - // - - idx++; events[idx].pos = r + 1, events[idx].eventType = DUMMY; } n *= 3; qsort(events, n, sizeof(Event), Compare); heapEnd = 0; for (i = 0; i < n; i++) { // Beginning of a poster, add to heap if (events[i].eventType == BEGIN) { Push(events[i].order); } // Mark topmost as visible if (heapEnd > 0) visible[heap[1]] = 1; if (events[i].eventType == END) { // Mark the poster as off-wall offWall[events[i].order] = 1; // Pop heap if the topmost poster is already off-wall while (heapEnd > 0 && offWall[heap[1]]) { Pop(); } } } n /= 3; for (ans = 0, i = 0; i < n; i++) ans += visible[i]; printf("%d\n", ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator