Color the Ball(懵逼题)
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 |
|
Sample Output
8 11 |
|
Author
ZHOU, Kai
|
|
Source
ZOJ Monthly, February 2005
|
|
Recommend
Ignatius.L
|
#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 ;
}
Color the Ball(懵逼题)的更多相关文章
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 线段树--Color the ball(多次染色问题)
K - Color the ball Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1199 Color the Ball
http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...
- Color the ball HDOJ--1556
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU-1556:Color the ball(前缀和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- GCD之信号量机制二
在前面GCD之信号量机制一中介绍了通过信号量设置并行最大线程数,依此信号量还可以防止多线程访问公有变量时数据有误,下面的代码能说明. 1.下面是不采用信号量修改公有变量的值 1 2 3 4 5 6 7 ...
- 【转】Keberos认证原理
前几天在给人解释Windows是如何通过Kerberos进行Authentication的时候,讲了半天也别把那位老兄讲明白,还差点把自己给绕进去.后来想想原因有以下两点:对于一个没有完全不了解Ker ...
- 【转】Mapreduce部署与第三方依赖包管理
Mapreduce部署是总会涉及到第三方包依赖问题,这些第三方包配置的方式不同,会对mapreduce的部署便捷性有一些影响,有时候还会导致脚本出错.本文介绍几种常用的配置方式: 1. HADOOP_ ...
- 记一次Java的内存泄露分析
当前环境 jdk == 1.8 httpasyncclient == 4.1.3 代码地址 git 地址:https://github.com/jasonGeng88/java-network-pro ...
- windown快速安装xgboost
记录xgboost的快速安装方式,该方式适合pyhton3.5/3.6版本. 系统: win10 64bit python版本:3.6 1. 下载xgboost编译好的whl包 下载路径为:http: ...
- Spring读书笔记——bean加载
我们的日常开发几乎离不开Spring,他为我们的开发带来了很大的便捷,那么Spring框架是如何做到方便他人的呢.今天就来说说bean如何被加载加载. 我们在xml文件中写过太多类似这样的bean声明 ...
- springMVC中的redirect和forward区别?
1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.
- C#获取本周第一天和最后一天
DateTime nowTime = DateTime.Now; #region 获取本周第一天 //星期一为第一天 int weeknow = Convert.ToInt32(nowTime.Day ...
- Docker入门系列(一):目标和安排
Docker入门系列(一) 这个系列的教程来源于docker的官方文档,此文档的目的在于一步一步学习docker的使用方法. 这一系列的教程有如下几篇文档: docker安装启动 构建第一个docke ...
- 将 C# 枚举反序列化为 JSON 字符串 基础理论
该转换过程需要引用 Newtonsoft.JSON,这其中的转换过程还是蛮有意思的. 一.定义枚举 /// <summary> /// 托寄物品枚举 /// </summary> ...