HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description
Fortunately, the method of unlocking the key to true Harmony is just
discovered by a group of philosophers. It is recorded on a strange meteorite
which has just hit the earth. You need to decipher the true meaning behind those
seemingly random symbols ... More precisely, you are to write a program which
will support the following two kinds of operation on an initially empty set S
:
1.
B X : Add number X to set S . The Kth command in the form of B X
always happens at time K , and number X does not belong to set S before this
operation.
2.
A Y : Of all the numbers in set S currently, find the one
which has the minimum remainder when divided by Y . In case a tie occurs, you
should choose the one which appeared latest in the input. Report the time when
this element is inserted.
It is said that if the answer can be given in
the minimum possible time, true Harmony can be achieved by human races. You task
is to write a program to help us.
Input
test case starts with one integer T where 1<=T<=40000 . The following T
lines each describe an operation, either in the form of ``B X " or ``A Y " where
1<=X , Y<=500000 .
T = 0 indicates the end of input file and should
not be processed by your program.
Output
indicated in the sample output. For every line in the form of ``A Y ", you
should output one number, the requested number, on a new line; output -1 if no
such number can be found. Separate the results of two successive inputs with one
single blank line.
Sample Input
Sample Output
题意
让你拯救世界
给定一空集合,有两种操作,往集合里加数或者求出集合中的数在mod k意义下最小值是第几个加入的,相等的话输出最后加入的
分析
不管怎么样先想一个暴力吧,单开一个数组记录往里边加的数,下边i表示加入时间,每次查询遍历一遍数组就行
#include<cstdio>
const int N=1e5+;
int a[N];
int main(){
char ss[];
int num,t,cas=;
while(~scanf("%d",&t)){
int len=;
if(t==)return ;
if(cas)printf("\n");
printf("Case %d:\n",++cas);
while(t--){
scanf("%s%d",ss,&num);
if(ss[]=='B')
a[++len]=num;
else {
bool flag=;
int Min=num,ans;
for(int i=;i<=len;i++){
if(a[i]%num<=Min){
flag=;
Min=a[i]%num;
ans=i;
}
}
if(flag)printf("-1\n");
else printf("%d\n",ans);
}
}
}
}
抱着试一试的心态交了上去,A了!!!没错它A了,但显然这不是正解,只是HDU数据水了,我自己手造了一组极限数据就跑了2s多,然后我又把它交到了POJ上,果然是TLE。
那肯定是要优化咯,怎么优化呢?涉及到mod的问题,如果要mod k在连续长度大于k的区间中,mod k一定会至少有两个数相等,所以我们可以考虑将50w分块,分为0-k-1,k-2k-1…………这样遍历每一块,每一块中最小的数mod k就有可能是答案,于是这个问题就成了,在区间内找到最小的已经出现过的数字,涉及到区间问题,很容易想到线段树和树状数组,但这个问题好像没有必要用线段树,树状数组足以。
开一棵50w的树状数组,每一个点记录到这个点一共出现了多少数字,接着就是找最小的已经出现过的数,那么不就又是暴力了吗?查找的方法除了暴力就只会二分,于是我们考虑二分查找,怎么二分呢?每次查找区间的时候,对该区间进行二分就行了,那你怎么知道是要向左分还是向右分?用树状数组很容易求得到区间左端点的点总数,取mid,如果到mid的点总数和到左端的点总数相等,说明l到mid之间没有数,改变左端点,如果有就改变右端点,更新最小值,继续二分直到l==r,树状数组的做法就是这样,线段树也大致差不多。
还有一个问题,如果k过小了,比如k就是1,那么我们就会将区间分成50w块,50w啊!而暴力最多只枚举4w,所以我们可以设置一个值,让k过大的时候直接用暴力。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int limit=;//这个数还可以换,让它别太小就行
//不要问我为什么非用这个奇怪的数
const int N=5e5+;
int q[N+],tim[N+],a[N+],len,c[N+];//多开5,防止RE
int lowbit(int i){
return i&(-i);
}
void Add(int x){
while(x<=N){
c[x]++;
x+=lowbit(x);
}
}
int n,k;
void Ins(int x){
tim[++len]=x;
a[x]=len;
for(int i=;i<=limit;i++){
if(q[i]==)q[i]=len;
else if(x%i<=tim[q[i]]%i)q[i]=len;//等于也要替换,因为优先输出后读入的
}
Add(x);//加到树状数组里统计前缀和
}
int front_sum(int x){
int sum=;
while(x){
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int low_find(int ll,int rr){
int l,r,Min=;
if(ll==)l=;
else l=ll;
if(rr>N)r=N;
else r=rr;
int pre=front_sum(l-);
while(l<=r){
int mid=l+r>>;
int now=front_sum(mid);
if(now>pre){//说明mid到l之间出现了值,向左找
r=mid-;
Min=mid;
}else l=mid+;//没有值就向右找
}
return Min;
}
void calc(int x){
if(len==){printf("-1\n");return;}
if(x<=limit){printf("%d\n",q[x]);return;}
int l=,r=x-,ans=x-;
while(l<=N){//判断左边界,判断右边界的话取值可能不完全
int now=low_find(l,r);
if(now&&(now%x<ans%x||now%x==ans%x&&a[now]>a[ans])){
//等于和小于两种情况分开写,合在一起不行
ans=now;
}
l+=x;r+=x;
}
printf("%d\n",a[ans]);
}
int main(){
int cas=;
while(~scanf("%d",&n)){
if(n==)return ;
for(int i=;i<=N;i++){
tim[i]=a[i]=c[i]=q[i]=;
}
len=;
if(cas)printf("\n");
printf("Case %d:\n",++cas);
for(int i=;i<=n;i++){
char ss[];
scanf("%s%d",ss,&k);
if(ss[]=='A')calc(k);
else Ins(k);
}
}
}
HDU 3303 Harmony Forever 前缀和+树状数组||线段树的更多相关文章
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- hdu 1166 敌兵布阵——(区间和)树状数组/线段树
pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...
- HDU 1166 敌兵布阵 树状数组||线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...
- Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化
题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$ $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...
随机推荐
- idea激活教程(永久)支持2019 3.1 亲测
此教程已支持最新2019.3版本 本教程适用Windows.Mac.Ubuntu等所有平台. 激活前准备工作 配置文件修改已经不在bin目录下直接修改,而是通过Idea修改 如果输入code一直弹出来 ...
- git还原历史某一版本
返回上一版本 git reset --hard HEAD^ 常用的命令git refloggit reset --hard "填写版本号" 黄色的就是版本号 其他查看以前版本的命令 ...
- Java对接微信登录
今天我们来对接微信开放平台的网站应用登录 首先上文档链接:https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login ...
- hive、Hbase、mysql的区别
1.Hive和HBase的区别 1)hive是sql语言,通过数据库的方式来操作hdfs文件系统,为了简化编程,底层计算方式为mapreduce. 2)hive是面向行存储的数据库. 3)Hive本身 ...
- 从头认识js-js的发展历史
JavaScript简介 JavaScript诞生于1995年,当时,它的主要目的是处理以前有服务端语言(如Perl)负责的一些输入验证操作. JavaScript简史 1995年2月当时就职于Net ...
- 7-3 jmu-python-回文数判断(5位数字) (10 分)
本题目要求输入一个5位自然数n,如果n的各位数字反向排列所得的自然数与n相等,则输出‘yes’,否则输出‘no’. 输入格式: 13531 输出格式: yes 输入样例1: 13531 输出样例1: ...
- vuex源码阅读分析
这几天忙啊,有绝地求生要上分,英雄联盟新赛季需要上分,就懒着什么也没写,很惭愧.这个vuex,vue-router,vue的源码我半个月前就看的差不多了,但是懒,哈哈.下面是vuex的源码分析在分析源 ...
- django models中字段
AutoField:一个自动递增的整型字段,添加记录时它会自动增长.你通常不需要直接使用这个字段:如果你不指定主键的话,系统会自动添加一个主键字段到你的model.(参阅自动主键字段) Boolean ...
- Ubuntu部署Asp.net core网站无法访问
前几天应工作需要,在阿里云上部署一个测试站点.本以为分分钟的事情,没想到打脸了. 当时直接新建一个webapi项目,publish后直接上传到阿里云,随后设置nginx转发网站端口5000. 接着打开 ...
- NoVNC安装部署
noVNC 是一个 HTML5 VNC 客户端,采用 HTML 5 WebSockets, Canvas 和 JavaScript 实现,noVNC 被普遍用在各大云计算.虚拟机控制面板中,比如 Op ...