迴圈

輸入

將時間小到大做排序

按照題目要求擺放

輸出

 

AC 程式碼:  (個人不是很喜歡,因為思路不清楚)

#include <cstdio>
#include <iostream>

using namespace std;

int N;
int group;
int trackID[25][8];
int playerID = 0;
double player[200][2];   // id && time

int main()
{
    while(scanf("%d", &N) != EOF)
    {
        group = N/8;
        for(int i = 0; i<N; i++)
            scanf("%lf%lf", &player[i][0], &player[i][1]);

        for(int i = 0; i<N-1; i++)
            for(int j = 0; j<N-i-1; j++)
                if(player[j][1] > player[j+1][1])
                    swap(player[j], player[j+1]);

        for(int i = 0; i<group; i++)
            trackID[i][3] = player[playerID++][0];
        for(int i = group-1; i>=0; i--)
            trackID[i][4] = player[playerID++][0];
        for(int i = 0; i<group; i++)
            trackID[i][2] = player[playerID++][0];
        for(int i = group-1; i>=0; i--)
            trackID[i][5] = player[playerID++][0];
        for(int i = 0; i<group; i++)
            trackID[i][1] = player[playerID++][0];
        for(int i = group-1; i>=0; i--)
            trackID[i][6] = player[playerID++][0];
        for(int i = 0; i<group; i++)
            trackID[i][0] = player[playerID++][0];
        for(int i = group-1; i>=0; i--)
            trackID[i][7] = player[playerID++][0];

        for(int i = 0; i<group; i++)
        {
            printf("%d ", i+1);
            for(int j = 0; j<8; j++)
                printf("%d ", trackID[i][j]);
            printf("\n");
        }
    }
    
    return 0;
}

 

TLE程式碼: (個人比較喜歡,因為思路清楚)

#include <iostream>

using namespace std;

 

 

int N;
int group;
int trackID[25][8];
double player[200][2];   // id && time

void setVar();
void quickSort(int left, int right);
void setPlayer();

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    while(cin >> N)
    {
        setVar();
        quickSort(0, N-1);
        setPlayer();

        for(int i = 0; i<group; i++)
        {
            cout << i+1 << " ";
            for(int j = 0; j<8; j++)
                cout << trackID[i][j] << " ";
            cout << endl;
        }
    }
}

void setVar()
{
    group = N/8;
    for(int i = 0; i<N; i++)
        cin >> player[i][0] >> player[i][1];
}

void quickSort(int left, int right)
{
    if(left >= right) return;

    int l = left+1;
    int r = right;
    double pivot = player[left][1];

    while(true)
    {
        while(l < right)
        {
            if(player[l][1] > pivot)
                break;
            l++;
        }

        while(r > left)
        {
            if(player[r][1] < pivot)
                break;
            r--;
        }

        if(l >= r) break;

        swap(player[l], player[r]);
    }

    swap(player[r], player[left]);

    quickSort(0, r-1);
    quickSort(r+1, right);
}

void setPlayer()
{
    int playerID = 0;

    for(int i = 0; i<group; i++)
        trackID[i][3] = player[playerID++][0];
    for(int i = group-1; i>=0; i--)
        trackID[i][4] = player[playerID++][0];
    for(int i = 0; i<group; i++)
        trackID[i][2] = player[playerID++][0];
    for(int i = group-1; i>=0; i--)
        trackID[i][5] = player[playerID++][0];
    for(int i = 0; i<group; i++)
        trackID[i][1] = player[playerID++][0];
    for(int i = group-1; i>=0; i--)
        trackID[i][6] = player[playerID++][0];
    for(int i = 0; i<group; i++)
        trackID[i][0] = player[playerID++][0];
    for(int i = group-1; i>=0; i--)
        trackID[i][7] = player[playerID++][0];
}

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()