HDU 1199

离散化原来就是指把每个线段单独提取出来,放到数组里。这样就不用考虑到它覆盖的范围了。

我用的是线段切割。。 做得还算顺利。 还可以用线段树做的~~

 

 

#include <cstdio> #include <algorithm> using namespace std; char op[3]; struct Interval { int start, end; bool operator < (const Interval& b) const{ if(start != b.start) { return start < b.start; } else { return end < b.end; } } }; Interval val[20001]; void white(int a, int b,int& cnt) { val[cnt].start = a; val[cnt].end = b; cnt++; } void black(int a, int b, int& cnt) { int tmp = cnt; for(int i = 0; i < cnt; i++){ if(val[i].start < a) { if( val[i].end >= a) { if(val[i].end <= b) { val[i].end = a-1; } else { val[tmp].start = b+1; val[tmp].end = val[i].end; tmp++; val[i].end = a-1; } } } else if(val[i].start <= b){ if(val[i].end <= b) { val[i].start = 0; val[i].end = -1; } else { val[i].start = b+1; } } } cnt = tmp; } int solve(int cnt, int& index) { sort(val, val+cnt); int max = val[0].end-val[0].start+1; for(int i = 1; i < cnt; i++) { if(val[i].start != 0) { if(val[i].start <= val[i-1].end+1) { if(val[i-1].end <= val[i].end) { val[i].start = val[i-1].start; } else { val[i].start = val[i-1].start; val[i].end = val[i-1].end; } } if(val[i].end-val[i].start+1 > max) { max = val[i].end-val[i].start+1; index = i; } } } return max; } int main() { int n, index, a, b; while(scanf("%d", &n) != EOF) { int cnt = 0; for(int i = 0; i < n; i++) { scanf("%d%d%s", &a, &b, op); if(a > b) { swap(a, b); } if(op[0] == 'w') { white(a, b, cnt); } else { black(a, b, cnt); } } index = 0; if(solve(cnt, index)) { printf("%d %d/n", val[index].start, val[index].end); } else { printf("Oh, my god/n"); } } return 0; }

你可能感兴趣的:(struct)