zoukankan      html  css  js  c++  java
  • 山东省第七届省赛 D题:Swiss-system tournament(归并排序)

    Description

    A Swiss-system tournament is a tournament which uses a non-elimination format. The first tournament of this type was a chess tournament in Zurich in 1895, hence the name "Swiss system". The tournament will be held based on following rules.

        2*N contestants (indexed 1, 2, ..., 2*N) will have R rounds matches. Before the first round, every contestant has an origin score. After every match, winner will get 1 score and loser will get 0 score. Before and after every round, contestants will be sorted by their scores in descending order. Two contestants with the same score will be sorted by their index with ascending order.

        In every round, contestants will have match based on the sorted list. The first place versus the second place, the third place versus the forth place, ..., the Kth place versus the (K + 1)th place, ..., the (2*N - 1)th place versus (2*N)th place.

       Now given the origin score and the ability of every contestant, we want to know the index of the Qth place contestant. We ensured that there won’t be two contestants with the same ability and the contestant with higher ability will always win the match.

    Input

     Multiple test cases. The first line contains a positive integer T (T<=10) indicating the number of test cases.

    For each test case, the first line contains three positive integers N (N <= 100,000), R (R <= 50), Q (Q <= 2*N), separated by space.

    The second line contains 2*N non-negative integers, s1, s2, ..., s2*N, si (si<= 108) indicates the origin score of constant indexed i.

    The third line contains 2*N positive integers, a1, a2, ..., a2*N, ai (ai<= 108) indicates the ability of constant indexed i.

    Output

     One line per case, an integer indicates the index of the Qth place contestant after R round matches.

    Sample Input

    1
    2 4 2
    7 6 6 7
    10 5 20 15

    Sample Output

    1

     

    题意:

    给出2*n个人,每次第1个人和第2个人比赛,第3个人和第4个人比赛…进行r轮比赛,每轮比完赛都进行排名,求出最后第q名是谁。给出每个人的积分 s[i],每个人的能力a[i],能力大的获胜,获胜的加1分,输了的不加分。

    题解:

    此题利用归并的思想,每轮比赛将赢者放一组,输者放一组,然后进行合并。

    代码:

    #include <bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <set>
    #include <bitset>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    #define is_lower(c) (c>='a' && c<='z')
    #define is_upper(c) (c>='A' && c<='Z')
    #define is_alpha(c) (is_lower(c) || is_upper(c))
    #define is_digit(c) (c>='0' && c<='9')
    #define min(a,b) ((a)<(b)?(a):(b))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define IO ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #define For(i,a,b) for(int i = a; i <= b; i++)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    typedef pair<ll,ll> pll;
    typedef vector<int> vi;
    const ll inf=0x3f3f3f3f;
    const double EPS=1e-10;
    const ll inf_ll=(ll)1e18;
    const ll mod=1000000007LL;
    const int maxn=1000000;
    const int N = 100000;
    struct node {
        int pos;
        int ability;
        int score;
    }q[2*N+5],Q[2*N+5];
    bool cmp(node a,node b)
    {
        if(a.score == b.score)
            return a.pos<b.pos;
        return a.score>b.score;
    }
    int n,r,p;
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int l1 = 1,r1 = 1;
            scanf("%d%d%d",&n,&r,&p);
            for(int i = 1; i <= n * 2; i++)
            {
                scanf("%d",&q[i].score);
                q[i].pos = i;
            }
            for(int i = 1; i <= n * 2; i++)
                scanf("%d",&q[i].ability);
            sort(q+1,q+1+2*n,cmp);
            for(int j = 1; j <= r; j++){
                l1 = 0;r1 = n;
                for(int i = 1; i <= 2 * n; i += 2)
                {
                    if(q[i].ability > q[i+1].ability) {
                        q[i].score++;
                        Q[++l1] = q[i];
                        Q[++r1] = q[i+1];
                    }else {
                        q[i+1].score++;
                        Q[++l1] = q[i+1];
                        Q[++r1] = q[i];
                    }
                }
                int flag = 0;
                int ll=1,rr=n+1;
                while(ll<=l1&&rr<=r1){
                    if(Q[ll].score == Q[rr].score)
                    {
                        if(Q[ll].pos<Q[rr].pos)
                            q[++flag] = Q[ll++];
                        else
                            q[++flag] = Q[rr++];
                    }else {
                        if(Q[ll].score>Q[rr].score)
                            q[++flag] = Q[ll++];
                        else
                            q[++flag] = Q[rr++];
                    }
                }
                while(ll<=l1)
                    q[++flag] = Q[ll++];
                while(rr<=r1)
                    q[++flag] = Q[rr++];
            }
            printf("%d
    ",q[p].pos);
        }
        return 0;
    }
     
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    [LeetCode]52. Bulls and Cows猜数字游戏
    [LeetCode]51. Ismorphic Strings同构字符串
    [LeetCode]50. Valid Anagram有效变位词
    [LettCode]49. Valid Sudoku有效数独
    [LeetCode]48. Word Pattern匹配模式
    Ubuntu 14.04中安装Sublime Text 3并使用SublimeClang插件
    [LeetCode]47. Integer to English Words整数的读法
    六度空间
    Saving James Bond
    列出图的连通集
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8591004.html
Copyright © 2011-2022 走看看