Test

Test

时间: 1ms        内存:128M

描述:

A vocational preference test, unlike an aptitude test, seeks to identify careers that the candidate might find satisfying. Based on the answers to a slew of seemingly inane multiple-choice questions like the one below, the test determines which occupations suit the candidate's personality.

   Which would you rather spend an afternoon doing?

   (a) feeding chickens
   (b) driving a race car
   (c) watching The Simpsons on TV
   (d) suntanning
   (e) building a dog house

Each question asks the candidate to express a preference from among five activities, selected from a common larger set. That is, activities like

feeding chickens

or

suntanning

are likely to appear in several different questions.

If a candidate answers A in a question containing A, B, C, D, E as alternatives, this choice indicates a preference for A over each of B, C, D, E. Also, if one answer indicates a preference for X over Y and one or more other answers indicate a preference for Y over Z, the combined set of answers indicates a preference for X over Z.

The candidate may provide contradictory answers; that is, the answers may indicate a preference for X over Y and also for Y over X. These contradictions indicate inconsistency, a personalitiy attribute that may suggest a career in politics or used auto sales.

Given a set of answers to a vocational preference test, you are to partition the activities into the minimal number of sets such that, for each pair within a given set, the answers indicate a contradictory preference.

The input contains several test cases followed by a line containing 0. Each case begins with n, the number of questions in the test. n lines follow, each containing the names of five distinct activities, followed by the candidate's answer - one of the five alternatives. Each activity is named by a single upper case letter.

For each test case, output the sets, one per line. Output the elements of each set in alphabetical order, and output the sets in alphabetical order by their least element. The sets should together contain exactly the set of activities that appear in the input. Leave an empty line between test cases.

输入:

Please Input Input Here

输出:

Please Input Output Here

示例输入:

4
A B C D E C
F C H I J J
K B H I F I
K C E B J K
0

示例输出:

A
B
C
D
E
F
H
I J K

提示:

参考答案(内存最优[864]):

#include <stdio.h>
#include <string.h>

char c[256][256], d[256], x[256];
int i,j,k,n,t;

main(){
   while (1 == scanf("%d",&n) && n) {
      if (t++) printf("\n");
      memset(c,0,sizeof(c));
      memset(d,0,sizeof(d));
      for (i=0;i<n;i++) {
         for (j=0;j<6;j++) scanf(" %c",&x[j]);
         for (j=0;j<6;j++) c[x[5]][x[j]] = 1;
         for (j=0;j<6;j++) d[x[j]] = 1;
      }
      for (i='A';i<='Z';i++) for (j='A';j<='Z';j++) for (k='A';k<='Z';k++)
         c[j][k] |= (c[j][i] && c[i][k]);
      for (i='A';i<='Z';i++) {
         if (!d[i]) continue;
         d[i] = 0;
         printf("%c",i);
         for (j=i+1;j<='Z';j++) {
            if (c[i][j] && c[j][i]) {
               printf(" %c",j);
               d[j] = 0;
            }
         }
         printf("\n");
      }
   }
   if (n) printf("missing end delimiter\n");
}

参考答案(时间最优[0]):

//  PR, meant to be immediate

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

using namespace std;

#define MAXN 200000

int main () {
  int n;
  int x[MAXN], y[MAXN];
  while (1) {
    scanf("%d",&n);
    if (!n) break;
    assert(0 < n && n%2 && n <= MAXN);
    int nn = (n-1)/2;
    int xb, yb;
    for(int i=0;i<n;i++) {
      scanf("%d%d",&x[i],&y[i]);
      if (i == nn) {
	xb = x[i]; yb = y[i];
      }
    }
    int ss = 0, os = 0;
    for (int i = 0; i<n; i++) {
      if (x[i] < xb && y[i] < yb || x[i] > xb && y[i] > yb) ss++;
      if (x[i] < xb && y[i] > yb || x[i] > xb && y[i] < yb) os++;
    }
    printf("%d %d\n",ss,os);
  }
  return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注