HDU 3303 Harmony Forever 前缀和+树状数组||线段树

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.

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.

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 2
0

Sample Output

Case 1:
1
2
1
Case 2:
1
 

题意

  让你拯救世界

  给定一空集合,有两种操作,往集合里加数或者求出集合中的数在mod k意义下最小值是第几个加入的,相等的话输出最后加入的

分析

  不管怎么样先想一个暴力吧,单开一个数组记录往里边加的数,下边i表示加入时间,每次查询遍历一遍数组就行

 

 1 #include
 2 const int N=1e5+10;
 3 int a[N];
 4 int main(){
 5     char ss[5];
 6     int num,t,cas=0;
 7     while(~scanf("%d",&t)){
 8         int len=0;
 9         if(t==0)return 0;
10         if(cas)printf("\n");
11         printf("Case %d:\n",++cas);
12         while(t--){
13             scanf("%s%d",ss,&num);
14             if(ss[0]=='B')
15                 a[++len]=num;
16             else {
17                 bool flag=1;
18                 int Min=num,ans;
19                 for(int i=1;i<=len;i++){
20                     if(a[i]%num<=Min){
21                         flag=0;
22                         Min=a[i]%num;
23                         ans=i;
24                     }
25                 }
26                 if(flag)printf("-1\n");
27                 else printf("%d\n",ans);
28             }
29         }
30     }
31 }

  抱着试一试的心态交了上去,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过大的时候直接用暴力。

  

 1 #include
 2 #include
 3 #include
 4 using namespace std;
 5 const int limit=1317;//这个数还可以换,让它别太小就行
 6                      //不要问我为什么非用这个奇怪的数
 7 const int N=5e5+10;
 8 int q[N+5],tim[N+5],a[N+5],len,c[N+5];//多开5,防止RE
 9 int lowbit(int i){
10     return i&(-i);
11 }
12 void Add(int x){
13     while(x<=N){
14         c[x]++;
15         x+=lowbit(x);
16     }
17 }
18 int n,k;
19 void Ins(int x){
20     tim[++len]=x;
21     a[x]=len;
22     for(int i=1;i<=limit;i++){
23         if(q[i]==0)q[i]=len;
24         else if(x%i<=tim[q[i]]%i)q[i]=len;//等于也要替换,因为优先输出后读入的
25     }
26     Add(x);//加到树状数组里统计前缀和
27 }
28 int front_sum(int x){
29     int sum=0;
30     while(x){
31         sum+=c[x];
32         x-=lowbit(x);
33     }
34     return sum;
35 }
36 int low_find(int ll,int rr){
37     int l,r,Min=0;
38     if(ll==0)l=1;
39     else l=ll;
40     if(rr>N)r=N;
41     else r=rr;
42     int pre=front_sum(l-1);
43     while(l<=r){
44         int mid=l+r>>1;
45         int now=front_sum(mid);
46         if(now>pre){//说明mid到l之间出现了值,向左找
47             r=mid-1;
48             Min=mid;
49         }else l=mid+1;//没有值就向右找
50     }
51     return Min;
52 }
53 void calc(int x){
54     if(len==0){printf("-1\n");return;}
55     if(x<=limit){printf("%d\n",q[x]);return;}
56     int l=0,r=x-1,ans=x-1;
57     while(l<=N){//判断左边界,判断右边界的话取值可能不完全
58         int now=low_find(l,r);
59         if(now&&(now%xa[ans])){
60         //等于和小于两种情况分开写,合在一起不行
61             ans=now;
62         }
63         l+=x;r+=x;
64     }
65     printf("%d\n",a[ans]);
66 }
67 int main(){
68     int    cas=0;
69     while(~scanf("%d",&n)){
70         if(n==0)return 0;
71         for(int i=0;i<=N;i++){
72             tim[i]=a[i]=c[i]=q[i]=0;
73         }
74         len=0;
75         if(cas)printf("\n");
76         printf("Case %d:\n",++cas);
77         for(int i=1;i<=n;i++){
78             char ss[5];
79             scanf("%s%d",ss,&k);
80             if(ss[0]=='A')calc(k);
81             else Ins(k);
82         }
83     }
84 }

你可能感兴趣的:(HDU 3303 Harmony Forever 前缀和+树状数组||线段树)