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
 

Source

和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];
					else
						ask[i].as=-1;
				}
			}
		}
		for(i=500000;i>=1;i--)
		{
			if(!v[i])
				fa[i]=fa[i+1];
			else
				fa[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);
				else
					printf("-1\n");
			}
		}
		scanf("%d",&n);
		if(n!=0)
			printf("\n");
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值