hdu 3303 Harmony Forever
来源:互联网 发布:python自学视频 编辑:程序博客网 时间:2024/06/17 22:30
Problem Description
We believe that every inhabitant of this universe eventually will find a way to live together in harmony and peace; that trust, patience, kindness and loyalty will exist between every living being of this earth; people will find a way to appreciate and cooperate with each other instead of continuous bickering, arguing and fighting. Harmony -- the stage of society so many people dream of and yet it seems so far away from now ...
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.
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
There are multiple test cases in the input file. Each 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.
T = 0 indicates the end of input file and should not be processed by your program.
Output
Print the result of each test case in the format as 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
5 B 1 A 5 B 10 A 5 A 40 2 B 1 A 20
Sample Output
Case 1: 1 2 1 Case 2: 1
Source
2006 Asia Regional Shanghai
和bzoj4320的输出要求稍微有一点不一样
大体做法是一样的
令r=sqrt(500000)
对于每个操作,直接维护y小于r的所有答案
然后对于y大于r的部分,我们可以暴力跑500000/y次
就是枚举y的各个倍数,然后求在数轴上这个数右边最近的数的位置
这里不知道支不支持Log的复杂度。
考虑离线,把添加变为删除,就可以直接用并查集维护把log去掉了
所以这题的总复杂度是O(nr)
----------------------------------------------------------------------------------------------------------
这题的每个数据是Case %d:而不是Case #%d:
然后最后一组数据似乎不需要在最后输出换行否则会PE
#include<cmath>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct question{int x,p;int as;}ask[40001];int loc[500011];int ans[40001];int ansx[40001];int fa[500011];bool v[500011];inline int find(int x){if(fa[x]!=x)fa[x]=find(fa[x]);return fa[x];}int main(){int n,kk=0;scanf("%d",&n);while(n!=0){kk++;int i,j;string x;int nt=sqrt(500000);memset(ans,127/3,sizeof(ans));memset(loc,127/3,sizeof(loc));memset(ask,127/3,sizeof(ask));memset(ansx,127/3,sizeof(ansx));memset(v,false,sizeof(v));memset(fa,0,sizeof(fa));int tot=0;for(i=1;i<=n;i++){cin>>x;if(x=="B"){tot++;ask[i].p=1;scanf("%d",&ask[i].x);for(j=1;j<=nt;j++){if(ask[i].x%j<=ans[j]){ans[j]=ask[i].x%j;ansx[j]=tot;}}v[ask[i].x]=true;loc[ask[i].x]=tot;}else{ask[i].p=2;scanf("%d",&ask[i].x);if(ask[i].x<=nt){if(ansx[ask[i].x]<=tot)ask[i].as=ansx[ask[i].x];elseask[i].as=-1;}}}for(i=500000;i>=1;i--){if(!v[i])fa[i]=fa[i+1];elsefa[i]=i;}for(i=n;i>=1;i--){if(ask[i].p==1)fa[ask[i].x]=fa[ask[i].x+1];else if(ask[i].x>nt){int minn=ask[i].as;for(j=0;j<=500000;j+=ask[i].x){int xt=find(j);if(j==0)xt=find(1);if(xt!=0){if(xt-j<minn){minn=xt-j;ask[i].as=loc[xt];}else if(xt-j<=minn&&loc[xt]>ask[i].as)ask[i].as=loc[xt];}//else//ask[i].as=-1;}}}printf("Case %d:\n",kk);for(i=1;i<=n;i++){if(ask[i].p==2){if(ask[i].as<=tot)printf("%d\n",ask[i].as);elseprintf("-1\n");}}scanf("%d",&n);if(n!=0)printf("\n");}return 0;}
0 0
- hdu 3303 Harmony Forever
- HDU 3303 Harmony Forever(数学)
- hdu Harmony Forever (线段树 )
- POJ 3145 HDU 3303 Harmony Forever 线段树 + 鸽巢定理
- POJ 3145 Harmony Forever
- POJ3145: Harmony Forever
- D POJ 3145 Harmony Forever
- POJ 3145 Harmony Forever 笔记
- poj 3145 Harmony Forever(线段树)
- poj 3145 Harmony Forever
- POJ-3145-Harmony Forever-线段树暴力
- poj3145 / hdu3303 Harmony Forever(树状数组 + 鸽巢原理)
- hdu 1866 A + B forever!
- HDU 1866 A + B forever!
- HDU 1866 A + B forever!
- Forever
- forever
- POJ 3145Harmony Forever(线段树更新+分情况处理数据+区间极值查询+好题)
- Javascript复习2之数据类型
- linux内核中的list
- phpcms首页输出组图
- android 把后台返回的数据中的逗号替换成空格
- Behavior Designer中文教程
- hdu 3303 Harmony Forever
- 常见的存储引擎
- JavaWeb 第12章 JDBC详解(三)
- Java运行时动态生成class的方法
- 从关注接口和父类(抽象类)的角度来提升阅读代码效率:
- maven的Module化的背景和方法:
- Runtime基本知识点以及应用场景
- AlexNet学习笔记-论文翻译和理解(二)
- 论转换器是定义成接口,然后用具体类实现,还是定义成一个静态方法: