Color the Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4244 Accepted Submission(s): 1055
There are multiple cases, process to the end of file.
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 3001;
struct node{
int left, right;
int iswhite;
}nd[MAXN];
int cnt;
int max(int a,int b){
return a>b?a:b;
}
bool cmp(const node &n1,const node &n2){
return n1.left < n2.left || (n1.left == n2.left&&n1.right < n2.right);
}
void update(int l,int r,char c){
if (c == 'w'){ //加上這個塗成白色的區間
nd[cnt].left = l;
nd[cnt].right = r;
nd[cnt].iswhite = 1;
cnt++;
}
else if (c == 'b'){ //對所有現有塗成白色區間的更新。
int i;
int k = cnt;
for (i = 0; i < k; i++){
if (nd[i].left < l&&nd[i].right > r){
nd[cnt].left = r + 1;
nd[cnt].right = nd[i].right;
nd[cnt].iswhite = 1;
nd[i].right = l-1;
cnt++;
}
else if (nd[i].left >= l&&nd[i].right <= r){
nd[i].iswhite = 0;
}
else if (nd[i].left<l&&nd[i].right>=l){
nd[i].right = l - 1;
}
else if (nd[i].left<=r&&nd[i].right > r){
nd[i].left = r + 1;
}
}
}
}
void getRes(){
int i, j;
int l,r;
int len=0; //白色區間的長度
l=r=-1;
for (i = 1; i < cnt; i++){
//printf("%d %d",nd[i-1].left,nd[i-1].right);
if (nd[i].iswhite == 1 && nd[i - 1].iswhite == 1){
if(nd[i].left >= nd[i - 1].left&&nd[i].left <= nd[i-1].right){
nd[i].left = nd[i - 1].left;
if (nd[i].right<nd[i-1].right){
nd[i].right = nd[i - 1].right;
}
nd[i-1].iswhite=0;
int a=nd[i].right-nd[i].left+1;
if(a>len){
l=nd[i].left;
r=nd[i].right;
len=a;
}
}else if(nd[i].left>nd[i-1].right){
int a=nd[i].right-nd[i].left+1;
int b=nd[i-1].right-nd[i-1].left+1;
if(a<=b){
if(b>len){
l=nd[i-1].left;
r=nd[i-1].right;
nd[i]=nd[i-1];
nd[i-1].iswhite=0;
len=b;
}
}else{
if(a>len){
l=nd[i].left;
r=nd[i].right;
len=a;
}
}
}
}
else if (nd[i - 1].iswhite == 1 && nd[i].iswhite == 0){
nd[i] = nd[i - 1];
nd[i-1].iswhite=0;
}else if(nd[i].iswhite==1&&nd[i-1].iswhite==0){
int a=nd[i].right-nd[i].left+1;
if(a>len){
l=nd[i].left;
r=nd[i].right;
len=a;
}
}
}
if (len==0){
printf("Oh, my god\n");
}
else
printf("%d %d\n", l, r);
}
int main(){
int n;
int a, b;
char c;
freopen("in.txt", "r", stdin);
while (~scanf("%d", &n)){
cnt = 0;
memset(nd, 0, sizeof(nd));
while (n--){
scanf("%d %d %c", &a, &b,&c);
update(a, b,c);
}
sort(nd, nd + cnt, cmp);
getRes();
}
return 0;
}