Color the Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5481 Accepted Submission(s): 1346
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.
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
只保存白色的求,然后用黑色区间更新白色球。(我用的结构体暴力求解)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 2005;
struct ss
{
int l, r;
bool operator< (const ss a) const
{
return l < a.l;
}
} L[MAX];
int N, l, r, k;
char wb;
int main()
{
while(scanf("%d", &N) != EOF)
{
k = 0;
while(N--)
{
scanf("%d %d %c", &l, &r, &wb);
if(l > r) swap(l, r);
if(wb == 'w')
{
L[k].l = l;
L[k++].r = r;
}
else
{
int k2 = k;
for(int i = 0; i < k2; ++i)
{
if(l <= L[i].l && r >= L[i].l && r < L[i].r) L[i].l = r+1;
else if(l > L[i].l && l <= L[i].r && r >= L[i].r) L[i].r = l-1;
else if(l > L[i].l && r < L[i].r)
{
L[k].l = r+1;
L[k++].r = L[i].r;
L[i].r = l-1;
}
else if(l <= L[i].l && r >= L[i].r)
{
L[i].l = -1;
L[i].r = -1;
}
}
}
}
sort(L, L+k);
int ans = 0, ansl, ansr;
l = -1;
r = -2;
for(int i = 0; i < k; ++i)
{
if(!L[i].l == -1) continue;
if(L[i].l > r+1)
{
if(r-l+1 > ans)
{
ansl = l;
ansr = r;
ans = r-l+1;
}
l = L[i].l;
r = L[i].r;
}
else r = max(r, L[i].r);
}
if(r-l+1 > ans)
{
ansl = l;
ansr = r;
ans = r-l+1;
}
if(ans == 0) cout <<"Oh, my god"<< endl;
else cout << ansl <<" "<< ansr << endl;
}
return 0;
}
本文介绍了一道关于寻找最长连续白色球序列的算法题目,通过记录和更新白色球区间来高效解决该问题。文章提供了完整的C++实现代码,利用结构体存储区间,并通过排序和区间合并等操作找到最长白色序列。

被折叠的 条评论
为什么被折叠?



