双指针合并
2024-07-25 15:09:00
发布于:广东
7阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
struct athl{
long long s,w,id;
}athlete[200009];
athl winner[200009];
athl loser[200009];
long long n,r,q;
bool cmp(athl a,athl b){
if(a.s!=b.s){
return a.s>b.s;
}
else{
return a.id<b.id;
}
}
void merge(){
long long idx=0;
for(int i=1;i<=2*n;i+=2){
idx++;
if(athlete[i].w>athlete[i+1].w){
athlete[i].s++;
winner[idx]=athlete[i];
loser[idx]=athlete[i+1];
}
else{
athlete[i+1].s++;
winner[idx]=athlete[i+1];
loser[idx]=athlete[i];
}
}
long long i=1,j=1;
long long idx2=0;
while(i<=idx&&j<=idx){
if(winner[i].s!=loser[j].s){
if(winner[i].s>loser[j].s){
idx2++;
athlete[idx2]=winner[i];
i++;
}
else{
idx2++;
athlete[idx2]=loser[j];
j++;
}
}
else{
if(winner[i].id<loser[j].id){
idx2++;
athlete[idx2]=winner[i];
i++;
}
else{
idx2++;
athlete[idx2]=loser[j];
j++;
}
}
}
while(i<=idx){
idx2++;
athlete[idx2] = winner[i++];
}
while(j<=idx){
idx2++;
athlete[idx2] = loser[j++];
}
}
int main(){
cin>>n>>r>>q;
for(int i=1;i<=2*n;i++){
cin>>athlete[i].s;
athlete[i].id=i;
}
for(int i=1;i<=2*n;i++){
cin>>athlete[i].w;
}
sort(athlete+1,athlete+2*n+1,cmp);
for(int i=1;i<=r;i++){
merge();
}
cout<<athlete[q].id;
return 0;
}
这里空空如也
有帮助,赞一个