归并排序的应用
先将其排序,后进行比赛会发现,赢得还是按分手从大到小, 输得也是,固然可以想到归并排序,将两个有序的数组和并。
归并排序参考:https://www.cnblogs.com/rstz/p/12393223.html
1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <vector> 5 using namespace std; 6 constexpr size_t maxn = 2e5 + 5; 7 struct node{ 8 int s, idx, w; 9 bool operator <(const node &t)const{//运算符重载 10 if(t.s != s) 11 return s > t.s; 12 return idx < t.idx; 13 } 14 15 }; 16 node p[maxn], winer[maxn], loser[maxn]; 17 int main(){ 18 int n, r, q; 19 cin >> n >> r >> q; 20 for(int i = 1; i <= 2 * n; ++ i) { 21 cin >> p[i].s; 22 p[i].idx = i; 23 } 24 for(int i = 1; i <= 2 * n; ++ i) cin >> p[i].w; 25 sort(p + 1, p + n*2 + 1); 26 while(r--){ 27 for(int i = 1; i <= n; ++ i){ 28 if(p[2*i - 1].w < p[2*i].w){ 29 p[2*i].s ++; 30 loser[i] = p[2*i-1]; 31 winer[i] = p[2*i]; 32 }else{ 33 p[2*i - 1].s++; 34 loser[i] = p[2*i]; 35 winer[i] = p[2*i - 1]; 36 } 37 } 38 merge(loser + 1, loser+n+1, winer+1, winer + n + 1 ,p + 1);//归并排序 39 } 40 cout << p[q].idx << endl; 41 return 0; 42 43 return 0; 44 }
追求吾之所爱