J - Scarily interesting!

J - Scarily interesting!

今天复习了一道思路题,题目不难。题目如下:

This year at Monsters University it is decided to arrange Scare Games. At the Games all campus gathers at the stadium stands, and the Scare program students divide into two teams to compete in their abilities of scaring children. This year the two teams will be “Oozma Kappa” and “Roar Omega Roar”.
Each team has n monsters, and the Games consist of n challenges. During each challenge Dean Hardscrabble, the chair of the Scare program, invites one monster from each team to demonstrate his mastery. Each of the monsters is invited only once and scores from 0 to 6 points, depending on how much a child is scared. The results of each challenge are announced at the same time for both monsters right after the end of this challenge. The winning team will be identified by the sum of the points scored by all its members.
Sports competition is an unpredictable process. But the Dean wants to keep all the course of the Games under control, so that the identity of the winning team will have been unclear for the audience as long as possible. For example, if six challenges until the end “Oozma Kappa” is forty points ahead, the audience at the stadium stands will just lose interest to the game. The Dean knows the skill level of all her students, and she wants to decide beforehand the order in which both teams’ members will be participating in the challenges. In what order should monsters from “Oozma Kappa” and from “Roar Omega Roar” show up to keep the audience in suspense as long as possible?
Input
The first line contains an integer n (2 ≤ n ≤ 1 000). The second line contains n integers within the range from 0 to 6, which are the points monsters from “Oozma Kappa” will score. The third line contains the points, monsters from “Roar Omega Roar” will score, written in the same manner.
Output
Output n lines, each containing integers oi and ri, which are the numbers of monsters from “Oozma Kappa” and “Roar Omega Roar” respectively, who should be called by the Dean to take part in the i-th challenge. In each team monsters are numbered with integers from 1 to n in the order they appear in the input data. If the problem has several solutions, output any of them.

题意:有一场pk赛,你是比赛的创办人,你提前知道两支队伍的每个成员会获得多少分,你要安排每支队伍人员的出场顺序,让比赛对于观众来说一直保持悬念(不能比赛还没比完,就已经分出胜负)。

思路:你只需要让能赢的队伍一直出分低的,而输的那只队伍一直出分高的即可。

AC代码

#include <bits/stdc++.h>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
    
    
    int data;
    int num;
} a[1010],b[1010],t;
int qsort(struct node a[],int n,int m)//快排
{
    
    
    int i=n,j=m;
    int x=a[n].data;
    t=a[n];
    if(i>=j)return 0;
    else
    {
    
    
        while(i<j)
        {
    
    
            while(i<j&&a[j].data>=x)j--;
            a[i]=a[j];
            while(i<j&&a[i].data<=x)i++;
            a[j]=a[i];
        }
    }
    a[i]=t;
    qsort(a,n,i-1);
    qsort(a,i+1,m);
    return 0;
}
int main()
{
    
    
    int n,i,suma=0,sumb=0;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
    
    
        scanf("%d",&a[i].data);
        a[i].num=i;
        suma+=a[i].data;
    }
    qsort(a,1,n);
    for(i=1; i<=n; i++)
    {
    
    
        scanf("%d",&b[i].data);
        b[i].num=i;
        sumb+=b[i].data;
    }
    qsort(b,1,n);
    if(suma>=sumb)
    {
    
    
        for(i=1; i<=n; i++)
            printf("%d %d\n",a[i].num,b[n+1-i].num);
    }
    else
    {
    
    
        for(i=1; i<=n; i++)
            printf("%d %d\n",a[n+1-i].num,b[i].num);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/rookie636/article/details/108194896