19.02.15 Test 3 题 (easy)

光盘

用一个栈存初始状态,两个栈模拟一下即可

#include<bits/stdc++.h>
#define N 100050
using namespace std;
struct STA{
	int s[N],top;
	void init(){top=0;}
	void Push(int i){s[++top] = i;}
	int Top(){return s[top];}
	void Pop(){ top--;}
}A,B;
int n,a[N],which[N];
int read(){
	int cnt=0,f=1; char ch=0;
	while(!isdigit(ch)){ch=getchar(); if(ch=='-') f=-1;}
	while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
	return cnt*f;
}
int main(){
	while(~scanf("%d",&n)){
		A.init(), B.init(); 
		for(int i=1;i<=n;i++){
			a[i] = read();
			which[i] = 1;
		}
		for(int i=n;i>=1;i--) A.Push(i);
		for(int i=1;i<=n;i++){
			if(which[a[i]] == 2){
				if(B.Top() != a[i]){printf("J\n"); goto die;}
				else B.Pop();
			} 
			else{
				while(A.Top()!=a[i]) 
					B.Push(A.Top()), which[A.Top()] = 2, A.Pop();
				A.Pop();
			}
		}
		printf("Y\n"); 
		die:;
	} return 0;
}

选点

每次枚举两个点,推出另外两个点,map判断存不存在

#include<bits/stdc++.h>
#define N 1050
using namespace std;
int read(){
	int cnt=0,f=1; char ch=0;
	while(!isdigit(ch)){ch=getchar(); if(ch=='-') f=-1;}
	while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
	return cnt*f;
}
map<pair<int,int>,int > M; 
int n,x[N],y[N],ans;
int check(int x,int y){return M[make_pair(x,y)];}
int main(){
	n = read();
	for(int i=1;i<=n;i++){
		x[i] = read(), y[i] = read();
		M[make_pair(x[i],y[i])] = 1;
	}
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			int x1 = x[i], y1 = y[i], x2 = x[j], y2 = y[j];
			if(check(x1+y1-y2, y1+x2-x1) && check(x2+y1-y2, y2+x2-x1)) ans ++;
			if(check(x1+y2-y1, y1+x1-x2) && check(x2+y2-y1, y2+x1-x2)) ans ++;
		}
	} printf("%d",ans/4); return 0;
}

rollcall

先离散化一下,然后将询问按pos排序,树状数组查询第k大就可以

#include<bits/stdc++.h>
#define N 30050
#define LL long long
using namespace std;
LL read(){
	LL cnt=0,f=1; char ch=0;
	while(!isdigit(ch)){ch=getchar(); if(ch=='-') f=-1;}
	while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
	return cnt*f;
}
int n,m,siz; LL a[N],b[N],c[N],ans[N];
struct Node{int pos,id;}q[N];
bool cmp(Node a,Node b){return a.pos<b.pos;}
void add(int x,LL val){for(;x<=siz;x+=x&-x) c[x] += val;}
int Kth(int k){
	int ans = 0, cnt = 0;
	for(int i=18;i>=0;i--){
		ans += (1<<i);
		if(ans > siz){ ans -= (1<<i); continue;}
		if(c[ans] + cnt < k) cnt += c[ans];
		else ans -= (1<<i);   
	} return ans+1;
}
int main(){
	n = read(), m = read();
	for(int i=1;i<=n;i++) a[i] = b[i] = read();
	sort(b+1,b+n+1); siz = unique(b+1,b+n+1) - (b+1);
	for(int i=1;i<=n;i++){
		a[i] = lower_bound(b+1,b+siz+1,a[i]) - b;
	}
	for(int i=1;i<=m;i++){
		q[i] = (Node){read(),i};
	} sort(q+1,q+m+1,cmp);
	int last = 0;
	for(int i=1;i<=m;i++){
		for(int j=last+1;j<=q[i].pos;j++) add(a[j],1);
		ans[q[i].id] = b[Kth(q[i].id)];
		last = q[i].pos;
	}
	for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FSYo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值