用一个栈存初始状态,两个栈模拟一下即可
#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;
}
先离散化一下,然后将询问按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;
}