poj__3145(配套总结本)
来源:互联网 发布:货运叫车软件 编辑:程序博客网 时间:2024/04/29 20:46
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 setS:
- B X: Add number X to set S. The Kth command in the form ofB X always happens at time K, and number X does not belong to setS before this operation.
- A Y : Of all the numbers in set S currently, find the one which has the minimum remainder when divided byY. 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 integerT 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 ≤ 500 000, 1 ≤ Y ≤ 1 000 000.
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
5B 1A 5B 10A 5A 402B 1A 20
Sample Output
Case 1:121Case 2:1
Source
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define LL long long#define pb push_back#define Set(a, v) memset(a, v, sizeof(a))#define For(i, a, b) for(int i = (a); i <= (int)(b); i++)#define Forr(i, a, b) for(int i = (a); i >= (int)(b); i--)#define MAXT (40000+5)#define MAXN (500000+5)#define INF 0x3f3f3f3fbool read(int &x){char ch = getchar();while(ch < '0' || ch > '9') ch = getchar();x = 0;while(ch >= '0' && ch <= '9'){x = x*10+ch-'0';ch = getchar();}return true;}int ql, qr;int id[MAXN];struct Seg_tree{int minv[MAXN*4];void init(){Set(minv, INF); Set(id, -1);}void insert(int o, int L, int R, int v){if(L == R){minv[o] = v; return;}int lc = o<<1, rc = o<<1|1;int mid = (L+R)>>1;if(minv[o] > v) minv[o] = v;if(v <= mid) insert(lc, L, mid, v);else insert(rc, mid+1, R, v);}int query(int o, int L, int R){ if(minv[o] == INF) return INF;if(ql <= L && qr >= R) return minv[o];int lc = o<<1, rc = o<<1|1;int mid = (L+R)>>1;int ret = INF;if(ql <= mid) ret = min(ret, query(lc, L, mid));if(qr > mid) ret = min(ret, query(rc, mid+1, R));return ret;}}ST;char op[5];int nn, num[MAXT];int main(){int n, T = 0, N = 500000;while(read(n) && n){ST.init(); nn = 0;if(T) printf("\n");printf("Case %d:\n", ++T);int x, minv, now, ans, L, R, tot = 0;For(ca, 1, n){scanf("%s", op); read(x);if(op[0] == 'B') ST.insert(1, 0, N, x), num[++nn] = x, id[x] = ++tot;else{if(x <= 5000){minv = INF, ans = -1;Forr(j, nn, 1){if(minv > (num[j]%x)) minv = num[j]%x, ans = j;if(!minv) break;}printf("%d\n", ans);}else{ minv = INF, ans = -1, L = 0;while(L <= N){R = L+x-1;if(R > N) R = N;ql = L; qr = R;now = ST.query(1, 0, N); if(now < INF && minv > (now%x)) minv = now%x, ans = id[now];else if(now < INF && minv == (now%x)) minv = now%x, ans = max(ans, id[now]);L = R+1;}printf("%d\n", ans); }}}}return 0;}
- poj__3145(配套总结本)
- 总结Spring Cloud各个组件配套使用
- 新版托业考试一本通(第3版) 配套cd
- 本教程为书籍配套教程,书籍名称:《从新手到高手:C++全方位学习》
- 本博客是非凡技术论坛www.feifanbbs.com的配套博客
- 爬取“好孩子”app的所有绘本、以及配套文件、音频(纯属自己娱乐)
- SQLITE3 使用总结本
- 第一本书总结~~~
- 留言本问题总结
- 面试配套
- 生活配套
- 本星期的一些总结
- 总结,获取本机号码
- 本学期数据结构学习总结
- 本ACM练习题结束总结
- 设计模式—策略模式—简单讲解(说讲解还不如是总结)—配套事例
- 总结从端到云的4层缓存机制,以及各层的工具,容器 和配套支撑
- pb12配套eas63
- Navicat for MySQL 11注册码
- 分贝(dB)的概念辨析
- MYSQL基础笔记
- mysql处理高并发,防止库存超卖(订单系统库存解决方案)
- NavigationView
- poj__3145(配套总结本)
- 众数问题
- Unity3D教程:Unity3D中网格合并示例
- mui 开发知识 积累总结-----图片压缩上传
- Java接口回调
- python实现带验证码网站的自动登陆
- java中标签的使用
- 第一次使用博客
- mongodb中用$set实现集合