zoukankan      html  css  js  c++  java
  • 珍珠 Median Weight Bead 977

    描述

    There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads:
    A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight.

    For example, the following results show which bead is heavier after M comparisons where M=4 and N=5.

    1.    Bead 2 is heavier than Bead 1.
    2.    Bead 4 is heavier than Bead 3.
    3.    Bead 5 is heavier than Bead 1.
    4.    Bead 4 is heavier than Bead 2.

    From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads.

    Write a program to count the number of beads which cannot have the median weight.

    输入

    The first line of the input file contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows:
    The first line of input data contains an integer N (1 <= N <= 99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead.

    输出

    There should be one line per test case. Print the number of beads which can never have the medium weight.

    样例输入

    1
    5 4
    2 1
    4 3
    5 1
    4 2

    样例输出

    2

    来源

    Tehran Sharif 2004 Preliminary

    题解

    翻译

    这种题拿到的第一反应也就是交给谷歌之类的吧…

    【问题描述】
    有N颗形状和大小都一致的珍珠,它们的重量不相同。N为整数,所有的珍珠从1到N编号。

    你的任务是发现哪颗珍珠的重量刚好处于正中间,即所有珍珠的重量中,该珍珠的重量列(N+1)/2位。

    下面给出将一对珍珠进行比较的方法:
    给你一架天平用来比较珍珠的重量,我们可以比出两个珍珠哪个更重一些,在作出一系列的比较后,我们可以将一些肯定不具备中间重量的拿走。
    例如,下列给出对5颗珍珠进行四次比较的情况:
    珍珠2比珍珠1重;
    珍珠4比珍珠3重;
    珍珠5比珍珠1重;
    珍珠4比珍珠2重;
    根据以上结果,虽然我们不能精确的的出哪个珍珠具有中间重量,但我们可以肯定珍珠1和珍珠4不可能具有中间重量,因为珍珠2,4,5比1重,而珍珠1,2,3比4轻,所以我们可以移走这两颗珍珠。
    写一个程序统计出共有多少颗珍珠肯定不会是中间重量。
    【输入格式】
    输入第1行表示有多组测试用例,每组测试用例第1行包含两个用空格隔开的整数N和M,其中1<=N<=99,且N为奇数,M表示对珍珠进行比较的次数,接下来M行每行包含两个用空格隔开的整数x和y,表示珍珠x比y重。
    【输出格式】
    每组测试用例,输出仅一行,包含一个整数,表示不可能是中间重量的珍珠总数
    【输入样例】
    1
    5 4
    2 1
    4 3
    5 1
    4 2
    【输出样例】

    2

    解析

    是一道Floyd算法可解的题。因为题目多源,故使用此算法。

    首先需要明确,当有一半珍珠比它重或一半珍珠比它轻的时候,那这枚珍珠也就没什么可能是中间那颗了。

    因此我们需要用Floyd算法为我们的邻接表做个排序,之后遍历统计比这枚珍珠重和轻的数量,再与半数Half相比即可。

     1 #include<iostream>
     2 #include<cstring>
     3 //Floyd
     4 using namespace std;
     5 const int MAXN=101;
     6 bool map[MAXN][MAXN];
     7 int count[MAXN];
     8 int main(){
     9     int t;
    10     scanf("%d",&t);
    11     int n,m;
    12     int a,b;
    13     int half;
    14     while(t--){
    15         memset(map,0,sizeof(map));
    16         memset(count,0,sizeof(count));
    17         scanf("%d%d",&n,&m);
    18         half=(n+1)/2;
    19         for(int i=1;i<=m;i++){
    20             scanf("%d%d",&a,&b);
    21             map[a][b]=true;//表示a珍珠比b珍珠要重 
    22         }
    23         for(int k=1;k<=n;k++){
    24             for(int i=1;i<=n;i++){
    25                 for(int j=1;j<=n;j++){
    26                     if(k!=i&&i!=j&&j!=k){
    27                         if(map[i][k]&&map[k][j]){
    28                             map[i][j]=true;
    29                         }
    30                     }
    31                 }
    32             }
    33         }
    34         for(int i=1,flag=0;i<=n;i++,flag=0){
    35             for(int j=1;j<=n;j++){
    36                 if(map[i][j]){
    37                     count[i]++;
    38                 }
    39             }
    40             if(count[i]>=half){
    41                 count[0]++;
    42             }
    43             else{
    44                 for(int j=1;j<=n;j++){
    45                     if(map[j][i]){
    46                         flag++;
    47                     }
    48                 }
    49                 if(flag>=half){
    50                     count[0]++;
    51                 }
    52             }
    53         }
    54         printf("%d
    ",count[0]);
    55     } 
    56 }
  • 相关阅读:
    JAVA实现文件遍历
    关于java的JIT知识
    经典的01背包问题
    文件遍历
    [算法之动态规划] 基本思想
    软件设计经典书籍推荐
    C++ 学习之函数重载、基于const的重载
    tomcat调用axis服务时出现 java.lang.InvocationException解决方案
    文件上传的原理
    文件上传的原理
  • 原文地址:https://www.cnblogs.com/Uninstalllingyi/p/10421947.html
Copyright © 2011-2022 走看看