HDU 1199 - Color the Ball 离散化
【题意】现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后找到最长的连续白色球,输出左右端点符号
【离散化】因为l[i]和r[i]都在int范围,显然不不可以开一个2^31-1那么大的数组。将l[i]和r[i]+1离散化,再模拟染色即可。
如果你不知道离散化:
将l[i]数组所有数与r[i]+1数组所有数取出来从小到大排序,做一个映射。
如样例
3
1 4 w
8 11 w
3 5 b
把1、5、8、12、3、6取出来,排序为1、3、5、6、8、12,离散化后
|
原数 |
1 |
3 |
5 |
6 |
8 |
12 |
|
离散化后 |
0 |
1 |
2 |
3 |
4 |
5 |
用mp[]做映射,类型为mp<int,int>。rev_mp[int]做逆向映射。
比如mp[4]=8,离散化后的4就可以看成数8,9,10,11的集合。如果离散化后的4被染成白色,那么相当于原数8,9,10,11均被染成白色。
再取样例中的一行: 1 4 w作为例子,这里1,4是原数,要从把球1,2,3,4均涂色,显然是凃离散化后0(1,2)和1(3,4)即可。
如果当初把r[i]做离散化而不是r[i]+1做离散化的话,r[i]就表示从它开始几个数的集合都被涂色,而不是从它结束涂色。
做好映射后,2^31-1个数就可以看成最多2*n个数,然后模拟染色即可。
这道题写的好晕啊,WA了5发后发现是多Case输入。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<string>
#include<sstream>
#define eps 1e-9
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 1005
#define MAXM 40005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
int i,j,k,n,m,x,y,T,ans,big,cas,num;
bool flag;
map <int , int> mp;
set <int> st; int l[],r[],rev_mp[];
char c[],color[];
int main()
{
while(~scanf("%d",&n))
{
st.clear();
mp.clear();
for (i=;i<=n;i++)
{
scanf("%d %d %c",&l[i],&r[i],&c[i]);
st.insert(l[i]);//现将其压入set中,也可以放入数组中最后排序
st.insert(r[i]+);
} num=;
for (set <int> ::iterator it=st.begin();it!=st.end();it++)//离散化
{
mp[*it]=num;//mp为map<int,int>类型,做一个映射,如上边的表格
rev_mp[num]=*it;//这是map的逆向映射。 num++;
} for (i=;i<num;i++) color[i]='b';//初始时为全部黑色 for (i=;i<=n;i++)//模拟涂刷过程
{
int left=mp[l[i]];
int right=mp[r[i]+];
for (j=left;j<right;j++)
{
color[j]=c[i];
}
} int pre=;
int left,right;
flag=false;
ans=;
for (i=;i<num;i++)//扫一遍寻找最长的连续白色球
{
if (color[i]!=color[i-])
{
if (color[i]=='w')
{
pre=i;//左端点
}else
if (color[i-]=='w' && ans < rev_mp[i] - rev_mp[pre] )
{
ans=rev_mp[i]-rev_mp[pre];//找到答案记录一下。
left=rev_mp[pre];
right=rev_mp[i]-;
flag=true;
}
}
}
if (!flag) printf("Oh, my god\n");else
printf("%d %d\n",left,right);
}
return ;
}
HDU 1199 - Color the Ball 离散化的更多相关文章
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...
- hdu 1199 Color the Ball
http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1199 Color the Ball 离散线段树
C - Color the Ball Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 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 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 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)To ...
- 线段树(求单结点) hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- ASP.NET MVC 缓存使用示例
应该说,缓存的设计是一门较为复杂的学问,主要考虑的问题包括:要不要缓存?要缓存哪些数据?要缓存多少数据?要缓存多久?如何更新缓存(手动还是自 动)?将缓存放在哪里?本文将以较为通俗易懂的方式,来看一看 ...
- 跨控制器操作-thinkphp
用A函数 或者 $use=new IndexController(); A跨控制器 $data->A("Admin/Index")//admin下面的index控制器 $da ...
- js Math函数
1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.fl ...
- Codeforces Round #313 A Currency System in Geraldion
A Currency System in Geraldion Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64 ...
- poj 2983Is the Information Reliable?
http://poj.org/problem?id=2983 #include<cstdio> #include<cstring> #include<algorithm& ...
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- 修改一行和修改全表的TX锁
SQL> select * from v$mystat where rownum<2; SID STATISTIC# VALUE ---------- ---------- ------- ...
- Currency Exchange(判断是否有正环)
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16456 Accepted: 5732 Description Seve ...
- 【动态规划】XMU 1560 新ACM规则
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1560 题目大意: 给定n(n<=200)个任务及每个任务的耗时,问m(m< ...
- hdu4746:2013杭州网络赛I 莫比乌斯反演
题意: 有5000组询问,每组询问求有多少i,j满足i∈[1,n],j∈[1,m]且gcd(i,j)的质因子数目<=p. n,m<=500000 思路: 首先预处理出每个数的质因子数目分别 ...