Color the Ball2
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 | Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4028 Accepted Submission(s): 999 Problem Description There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b' , 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence. Input First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b' . There are multiple cases, process to the end of file. Output Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god" . Sample Input 3 1 4 w 8 11 w 3 5 b Sample Output 8 11 |
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<cstring> using namespace std; struct node{ int beg, en; }s[8000]; int len;//维持线段长度 bool cmp(node a, node b){ if (a.beg != b.beg) return a.beg < b.beg;//从小到大排有利于寻找可连接的段 else return a.en>b.en; } void Process(int h, int e, char c){ //只对将白涂黑进行处理,总共有4种情况 //离散区间为[ ] if (c == 'w'){ s[len].beg = h; s[len++].en = e; } else{ for (int i = 0; i < len; i++){ if (s[i].beg >= h&&s[i].en <= e){ s[i].beg = s[i].en = -1; } else if (s[i].beg >= h&&s[i].en >= e&&s[i].beg <= e){ s[i].beg = e + 1; } else if (s[i].beg <= h&&s[i].en >= e){ s[len].en = s[i].en;//先这一步,否则会被覆盖,^v^ s[i].en = h - 1; s[len++].beg = e + 1; } else if (s[i].beg <= h&&s[i].en >= h&&s[i].en<=e){ s[i].en = h - 1; } } } } int main() { int cnt, n, h, e, x, y; char c; while (scanf("%d", &n) != -1){ memset(s, 0, sizeof(s)); len = 0; for (int i = 0; i < n; i++){ scanf("%d %d %c", &h, &e, &c); Process(h, e, c); } cnt = h = e = -1; sort(s, s + len, cmp); for (int i = 0; i < len; i++){ if (s[i].beg != -1){ if (s[i].beg <= e + 1){ e = max(e, s[i].en); } else{ h = s[i].beg; e = s[i].en; } if (cnt < e - h + 1){ cnt = e - h + 1; x = h; y = e; } } } if (cnt == -1){ printf("Oh, my god\n"); } else{ printf("%d %d\n", x, y); } } return 0; }
世上无难事,只要肯登攀。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步