codechef营养题 第三弹

第三弾が始まる!

codechef problems 第三弹

一、Motorbike Racing

题面

It‘s time for the annual exciting Motorbike Race in Byteland.

There are N motorcyclists taking part in the competition. Johnny is watching the race. At the present moment (time 0), Johnny has taken note of the current velocity and position of each motorcyclist.

Johnny wants to know at a given point of time, which motorcyclist is in a specific place in the rank list. Please help him!

If at any given time two motorcyclists are in same position, the motorcyclist with the smaller index will be placed before the one with the larger index.

To make the problem simple, Johnny assumes that each motorcyclist is moving at a constant velocity.

Input

The first line contains a number t (about 10) which is the number of test cases. Then t test cases follow. Each test case has the following form.

The first line of the test case contains a number N (1 <= N <= 2000), the number of motorcyclists.

The i-th line in the next N lines contains two numbers, v and x, which are the velocity and the current position of the i-th motorcyclist (1 <= v, x <= 100,000).

The next line contains a number Q (1 <= Q <= 2000), the number of time queries.

Each line in the next Q lines contains two numbers, t (1 <= t <= 1,000,000,000) and k (1 <= k <= n), representing the query: "at time t, which motorcyclist is positioned k-th in the rank list?"

Output

For each test case, print Q lines, with each line containing the index of the motorcyclist for the corresponding query.

Remember to print a new line after each test case.

Example

Input:
1
4
2 100
3 50
4 60
5 1
4
1 1
50 2
60 4
100 1
Output:
1
4
1
4

Description

一列摩托车,描述为二元组,(速度,初始位移)

给出一些询问,问在某个时刻下,位移第k大的摩托的编号

Solution

暴力去修改,之后全部排序的话,O(nqlogn),超时

部分排序的话,用nth_element可以水过,而且很快

这里的解给出的是O(nq+n*n)的

即,离线来做

考虑到车子的变化是有限的,即超车次数不超过n*n

我们对询问排序,每次对每辆车冒泡

这样就可以正确过了

codechef上目前排名第5 哦

rank 5 code

#include <stdio.h>
#include <algorithm>
#define RG register
#define N 2010
#define MAXBUF 1<<22
#define L long long
#define gec() ((S==T&&(T=(S=B)+fread(B,1,MAXBUF,stdin),S==T))?0:*S++)
#define puc() fwrite(buff,1,itrr-buff,stdout),itrr=buff;
char B[MAXBUF],*S=B,*T=B,buff[MAXBUF],*itrr=buff;int stt[110];
template<class Type>inline void Rin(RG Type &aa){
	aa=0;bool bb=0;char cc;
	for(cc=gec();(cc<‘0‘||cc>‘9‘)&&cc!=‘-‘;cc=gec())
		;
	for(cc==‘-‘?bb=1:aa=cc-‘0‘,cc=gec();‘0‘<=cc&&cc<=‘9‘;cc=gec())
		aa=aa*10+cc-‘0‘;
	aa=bb?-aa:aa;
}
template<class Type>inline void Cat(RG Type aa,RG char cc=‘\n‘){
	RG int O=0;
	if(!aa)
		*itrr++=‘0‘;
	else{
		aa<0?aa=-aa,*itrr++=‘-‘:1;
		for(;aa;stt[++O]=aa%10,aa/=10)
			;
		for(;O;*itrr++=‘0‘+stt[O--])
			;
	}
	*itrr++=cc;
}
int kase,n,m,ans[N];
struct bike{
	int v,x,ind; L now;
	bool operator < (bike &o)const{
		return now>o.now||now==o.now&&ind<o.ind; } }a[N];
struct resq{
	int t,k,ind;
	bool operator < (resq &o)const{
		return t<o.t; } }b[N];
#define set_file(File) freopen(#File".in","r",stdin),freopen(#File".out","w",stdout)
#define close_file() fclose(stdin),fclose(stdout)
int main(){
	set_file(cc mot rac);
	Rin(kase);
	while(kase--){
		Rin(n);
		for(RG int i=1;i<=n;i++)
			Rin(a[i].v),Rin(a[i].x),a[i].ind=i;
		Rin(m);
		for(RG int i=1;i<=m;i++)
			Rin(b[i].t),Rin(b[i].k),b[i].ind=i;
		std::sort(b+1,b+1+m);
		for(RG int i=1;i<=n;i++)
			a[i].now=(L)a[i].v*b[1].t+a[i].x;
		std::sort(a+1,a+1+n);
		ans[b[1].ind]=a[b[1].k].ind;
		for(RG int i=2;i<=m;i++){
			RG int t=b[i].t,k=b[i].k;
			for(RG int j=1;j<=n;j++)
				a[j].now=(L)a[j].v*t+a[j].x;
			for(RG int j=2;j<=n;j++){
				RG int x=j;
				while(x>1&&a[x]<a[x-1]){
					bike tmp=a[x];
					a[x]=a[x-1];
					a[x-1]=tmp;
					x--;
				}
			}
			ans[b[i].ind]=a[k].ind;
		}
		for(RG int i=1;i<=m;i++)
			Cat(ans[i]);
		if(kase)
			*itrr++=‘\n‘;
	}
	puc();
	close_file();
	return 0; }

  

时间: 2024-08-25 03:49:53

codechef营养题 第三弹的相关文章

codechef 营养题 第一弹

第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5XXoERDanUcdxw08SmRj1a5VY1o7jpW1xYv_V1kuYao1Pg4yKdfG4MfNsNAEa codechef problems 第一弹 一.Authentication Failed原题题面Several days ago Chef decided to registe

codechef 两题

前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM,不过他求得是XOR最大值,原理类似.. B:KMP居然写搓了,后来一直改,题目放个链接好了:http://www.codechef.com/LTIME14/problems/TASHIFT. 我么可以对B字符串复制一下,然后再对A字符串求出NEXT数组,再匹配的过程中求出匹配最大长度时的位置, 刚开始我没想

日均百万PV架构第三弹(分布内容为王)

接续接上篇 缓存时代来临 为蓝本,继续改造我们的百万级站点架构,这次我们 拿之前存储静态内容的 nfs 开刀,众所周知 nfs 的多台集群节点下可能由于多重 原因(磁盘io , 网络带宽, 并发场景),不适合做文件共享系统的基础结构. 互联网站点中,存在大量图片或其他静态内容,并且这些内容一般在1M之内,对于 海量小文件,我们将采用mogilefs分布式文件系统来完成.其中概念自行google. # mogilefs分布式文件系统工作流程 架构已经愈发复杂,我们需要从新梳理一下.从下表中应该很容

《我与希乐仑》第三弹

此案编号:黄劳人仲 (2014) 办字 第518号 开庭前,发生了一个戏剧性的场景,对方来了三个人,一位是外服的李盼,一位是徐敏,但还有一位叫王霞的,尽然说是Selerant的HR,扯淡嘛!此人来审判庭的目的非常值得怀疑,首先我有充分的证据证明她不属于Selerant,很简单!我们可以去调阅她的人事档案,至少在2014年3月26日当天,她肯定不属于Selerant,因为我清楚地记得她是徐敏的朋友,所以罗,纯粹是打酱油的,也或许是来拉关系的.你说一个不相干的人,庭上基本没说话,那她来干什么?这又不

深究angularJS系列 - 第三弹

深究angularJS系列 - 初识 深究angularJS系列 - 第二弹 深究angularJS系列 - 第三弹,我们一起深入探究angular的服务和自定义指令O(∩_∩)O~~ Angular服务 $http: $http是angular中的一个核心服务; $http利用浏览器的xmlhttprequest或JSONP与远程HTTP服务器进行交互; $http的支持多种method的请求,get.post.put.delete.jsonp等. 下面通过JSONP方法进行$http服务的使

Android Window PhoneWindow Activity学习心得--第三弹

Android Window  PhoneWindow Activity学习心得--第三弹 前面 我们完成了从Activity到PhoneWindow的整体跨度 正如我们所知道的与Activity组件关联的一个应用程序窗口视图对象关联一个ViewRoot对象,而将 一个Activity组件的应用程序窗口视图对象与一个ViewRoot对象关联是通过该Activity组件所使用的 窗口管理器(WindowManager)来执行的. 在我们初始化DecorView完成之后,我们需要关联应用程序窗口视图

矩阵十题【三】 HDU 1588 Gauss Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1588 题目大意:先要知道一组斐波那契数列 i 0 1 2 3 4 5 6 7 f(i) 0 1 1 2 3 5 8 13 下面给你一组数: k,b,n,M 现在知道一组公式g(i)=k*i+b:(i=0,1,2,3...n-1) 让你求出 f(g(i)) 的总和(i=01,2,3,...,n-1),比如给出的数据是2 1 4 100 2*0+1=1   f(1)=1 2*1+1=3   f(3)=2

[爱上Swift]第三弹:使用Swift建立App基本基石

搭架子 首先这次我们会主要使用IOS自带的导航Controller为一个APP建立一个简单的基石, 新建一个空的Application并创建3个swift文件,分别命名为:FirstViewController,SecondViewController,ThirdViewController; 同时在三个Swift的Controller中重写继承类的viewDidLoad()方法: override func viewDidLoad(){ super.viewDidLoad(); } 在整个程序

Android 特效View第三弹之闪烁View

Android  特效View第三弹之闪烁View 动态效果图我只做了半天还是失败了,给一个截图,剩下的全靠想象了 <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.empty.FlickerTextView android:id="@+id/flicker" android:l