hdu 1199 Color the Ball

ゝ一世哀愁。 2022-08-03 12:41 276阅读 0赞

Color the Ball

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5010 Accepted Submission(s): 1243

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

  1. 3
  2. 1 4 w
  3. 8 11 w
  4. 3 5 b

Sample Output

  1. 8 11

Author

ZHOU, Kai

Source

ZOJ Monthly, February 2005

写的时候错误百出啊,,

  1. //hdu 1199
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<string>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12. #include<set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. using namespace std;
  16. typedef long long ll;
  17. typedef pair<int,int> pii;
  18. inline int in()
  19. {
  20. int res=0;char c;
  21. while((c=getchar())<'0' || c>'9');
  22. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  23. return res;
  24. }
  25. struct st
  26. {
  27. int l;int r;
  28. }a[22222]; //要大一点,因为有很多会分成两个的
  29. int main()
  30. {
  31. int n;
  32. while(~scanf("%d",&n))
  33. {
  34. int k=0;
  35. for(int i=0;i<n;i++)
  36. {
  37. int l=in(),r=in();
  38. char c=getchar();
  39. if(c=='w')
  40. {
  41. a[k].l=l;
  42. a[k++].r=r;
  43. }
  44. if(c=='b')
  45. {
  46. int tmp=k;
  47. for(int j=0;j<tmp;j++) //4种情况
  48. {
  49. if(a[j].l>=l && a[j].r<=r) a[j].l=-10,a[j].r=-inf;
  50. else if(a[j].l<l && a[j].r>=l && a[j].r<=r ) a[j].r=l-1;
  51. else if(a[j].l<=r && a[j].l>=l && a[j].r>r ) a[j].l=r+1;
  52. else if(a[j].l<l && a[j].r>r )
  53. {
  54. a[k].l=r+1;
  55. a[k++].r=a[j].r;
  56. a[j].r=l-1;
  57. }
  58. }
  59. }
  60. else
  61. {
  62. for(int j=0;j<k;j++) //两种情况就行
  63. {
  64. if(a[j].l<l && a[j].r>=l && a[j].r<=r ) l=a[j].l; //要更新l,而不是a[j].l
  65. else if(a[j].l>=l && a[j].l<=r && a[j].r>r ) r=a[j].r;//只更新a[j]的话可能造成漏掉一些后面能满足情况的
  66. }
  67. a[k-1].l=l; //将当前的节点也更新
  68. a[k-1].r=r;
  69. }
  70. }
  71. int ans1=0,ans2=-1;
  72. for(int i=0;i<k;i++)
  73. {
  74. if(a[i].r-a[i].l>ans2-ans1)
  75. {
  76. ans1=a[i].l;
  77. ans2=a[i].r;
  78. }
  79. }
  80. if(ans1==0 && ans2==-1)puts("Oh, my god");
  81. else
  82. printf("%d %d\n",ans1,ans2);
  83. }
  84. return 0;
  85. }

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读