Color the Ball

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 48 Accepted Submission(s): 25
 
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
 
Author
ZHOU, Kai
 
Source
ZOJ Monthly, February 2005
 
Recommend
Ignatius.L
 
因为这个专题都是线段树嘛,刚开始离散化进行区间合并,但是写着写着就爆了,后来又想了一下,不用合并,只要
set一下记录区间就行,但是边界问题各种wa,没忍住看了一发题解,看到暴力可以搞,结果就暴力一发就过了.......
就过了......
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define INF 0x3f3f3f3f
char a[];//数组开小了,wa了几次
int n,x,y;
char ch;
void init(){
memset(a,'b',sizeof(a));
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
init();
int maxnum=,minnum=INF;//数据的范围
for(int i=;i<n;i++){
cin>>x>>y>>ch;//cin处理方便的点
if(y>maxnum)
maxnum=y; if(x<minnum)
minnum=x; for(int j=x;j<=y;j++)//模拟染色
a[j]=ch; }
int sum=;//记录连续白球的个数
int max1=;//记录最多连续白球个数
int beg=minnum,end=minnum;//记录连续白球的区间
int minpos=-,maxpos=-;//最大连续白球的左右区间 for(int i=minnum;i<=maxnum+;i++){
if(a[i]=='b'){//如果当前位置是黑球的话
if(sum>max1){
max1=sum;
minpos=beg;
maxpos=end;
}
sum=;
continue;
}
if(sum==)
beg=i;
sum++;
end=i; }
if(max1==)
cout<<"Oh, my god"<<endl;
else
cout<<minpos<<" "<<maxpos<<endl; }
return ;
}

经过离散化处理的线段树

/*
* @Author: lyucheng
* @Date: 2017-07-03 21:05:51
* @Last Modified by: lyucheng
* @Last Modified time: 2017-07-05 15:45:24
*/
/*
题意: 最长连续子序列的长度 思路:线段树的区间set问题 错误:离散化出了问题,如果单纯的去重,会产生这种错误:
input
2
1 3 w
5 5 w
exception output:
1 5
right output:
1 3
所以离散化的时候要改进:
数据进行离散化的时候,开区间离散化端点,闭区间一般用两个点离散一个端点.例如:
开区间 [1.5] 实际的离散化之后应该是 [ (1,2) , (4,5) ].
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
#define LL long long
#define MAXN 50000+5
#define lson i*2,l,m
#define rson i*2+1,m+1,r using namespace std; int n;
LL l[MAXN],r[MAXN];
char str[MAXN][];
LL X[MAXN*];
int len;
int k;
int cover[MAXN*];
int setd[MAXN*]; void pushup(int i,int l,int r){
if(setd[i*]==-||setd[i*+]==-){
setd[i]=-;
}else if(setd[i*]!=setd[i*+]){
setd[i]=-;
}else {
setd[i]=setd[i*];
}
} void pushdown(int i,int l,int r){
if(setd[i]!=-){
setd[i*]=setd[i*+]=setd[i];
setd[i]=-;
}
} void build(int i,int l,int r){
setd[i]=;
if(l==r) return ;
int m=l+(r-l)/;
build(lson);
build(rson);
} void update(int ql,int qr,int val,int i,int l,int r){ if(ql<=l&&r<=qr){
setd[i]=val;
pushdown(i,l,r);
return ;
} if(setd[i]==val) return ;//没有继续更新的必要 if(l==r) return ; pushdown(i,r,l); int m=l+(r-l)/;
if(m>=ql) update(ql,qr,val,lson);
if(m<=qr) update(ql,qr,val,rson);
// pushup(i,l,r);
} void query(int ql,int qr,int i,int l,int r){
if(setd[i]!=-){
for(int j=l;j<=r;j++){
cover[j]=setd[i];
}
return ;
}
int m=l+(r-l)/;
if(m>=ql) query(ql,qr,lson);
if(m<=qr) query(ql,qr,rson);
} int findx(LL key){
int l=,r=k,mid;
while(l<=r){
mid=l+(r-l)/;
if(X[mid]==key){
return mid;
}else if(X[mid]>key){
r=mid-;
}else{
l=mid+;
}
}
return -;
}
void init(){
len=;
k=;
memset(cover,,sizeof cover);
} int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){ init();
for(int i=;i<n;i++){
scanf("%lld%lld%s",&l[i],&r[i],str[i]);
X[++len]=l[i];
X[++len]=r[i];
} //处理开区间
sort(X+,X+len+);
int t=len;
for(int i=;i<=t;i++){
if(X[i]-X[i-]>) X[++len]=X[i-]+;
if(X[i]-X[i-]>) X[++len]=X[i]-;
} //去重
sort(X+,X+len+);
for(int i=;i<=len;i++){
if(X[i]!=X[i-]){
X[++k]=X[i];
}
}
// for(int i=1;i<=k;i++){
// cout<<X[i]<<" ";
// }cout<<endl;
// cout<<"k="<<k<<endl;
build(,,k); for(int i=;i<n;i++){
int fl=findx(l[i]);
int fr=findx(r[i]);
if(str[i][]=='w')
update(fl,fr,,,,k);
else
update(fl,fr,,,,k);
}
// cout<<"ok"<<endl;
query(,k,,,k);//更新到cover数组中 // for(int i=1;i<=k;i++){
// cout<<cover[i]<<" ";
// }cout<<endl; LL _max=,_maxl,_maxr;
int i=;
while(i<=k){
if(cover[i]==){//白色气球
int j=i+;
while(cover[j]==&&j<=k) j++;
// cout<<X[j-1]<<" "<<X[i]<<endl;
if(X[j-]-X[i]+>_max){
_max=X[j-]-X[i]+;
_maxl=X[i];
_maxr=X[j-];
}
i = j;
}else{
i++;
}
// cout<<_max<<endl;
}
if(_max==){
puts("Oh, my god");
}else{
printf("%lld %lld\n",_maxl,_maxr);
}
}
return ;
}

最新文章

  1. SQL转换时间的时分
  2. jQuery取得select 选中值和文本 来自园友“大气象”
  3. Putty SSH简单使用
  4. mybatis的逆向工程
  5. js-统计选项个数
  6. SpringMVC整合MongoDB开发 架构搭建
  7. 使用mobile.changePage()时出现的问题(转)
  8. twitter storm 源码走读之5 -- worker进程内部消息传递处理和数据结构分析
  9. SqlSever基础 delete 删除符合特定条件的元素
  10. easyui中jquery重复引用问题(tab内存泄露问题)
  11. UIView的Touch事件UIControlEvents详解
  12. Mysql shell 控制台---mysqlsh
  13. Mule与其它web应用服务器的区别
  14. jQuery中find()和filter()的区别
  15. 神州数码品众_Android面试
  16. Mybatis3.2.1整合Spring3.1
  17. Lua性能优化
  18. Hibernate学习---单表查询
  19. 如何配置使用HTML在线编辑工具
  20. 在CentOS7服务器端启动jupyter notebook服务,在windows端使用jupyter notebook,服务器充当后台计算云端

热门文章

  1. windows10 下安装node失败 出现2502 2503的解决办法
  2. 7-21(排序) PAT排名汇总
  3. hibernate学习手记(2)
  4. Javascript写的一个可拖拽排序的列表
  5. 集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序
  6. Django进阶篇【2】
  7. PHP通过访客来路获取搜索关键词的方法
  8. hadoop配置文件详解,安装及相关操作
  9. javascript小节
  10. C语言第一次实验报告————PTA实验1.2.3内容