toj 2282: Median Weight Bead (队列+vector)

描述

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

一组中第一个重于第二个,求有多少珠子不能作为中间重量的珠子

思路:队列。

直接把重量看成编号

一个qian【101】的vector和一个hou【101】的vector,分别用来存放某个数前面和后面的数。

一共有n个数,所以for循环 n次,每次把qian【该数】里的数字进队,标记,算个数,再把队列里的数的qian数组里的数遍历掉

如果个数大于n的一半,就直接不行了。否则再判断hou数组里的(和qian的一样,复制粘贴就行

#include<bits/stdc++.h>
using namespace std;
vector<int>qian[101],hou[101];
queue<int>qu;
int main()
{
    int n,m,i,t,j;
    cin>>t;
    while(t--)
    {
        for(i=0;i<101;i++)
        {
            qian[i].clear();
            hou[i].clear();
        }
        cin>>n>>m;
        int ban=n/2;
        while(m--)
        {
            int x,y;//假设重量从前往后排
            cin>>x>>y;//x比y重
            qian[y].push_back(x);//排在y前面的进去
            hou[x].push_back(y);//排在x后面的进去
        }
        while(!qu.empty())
        {
            qu.pop();
        }
        int visted[101];
        int summ=0;
        for(i=1;i<=n;i++)//n次
        {
            int sum=0;
            memset(visted,0,sizeof(visted));
            for(j=0;j<qian[i].size();j++)
            {
                int p=qian[i][j];
                qu.push(p);
            }
            while(!qu.empty())
            {
                int q=qu.front();
                if(visted[q]==0)
                {
                    visted[q]=1;
                    sum++;
                }
                for(int k=0;k<qian[q].size();k++)
                {
                    if(visted[qian[q][k]]==0)qu.push(qian[q][k]);//一定要判断是否进过队列,进过就不用再push进去了
                }
                qu.pop();
            }
            if(sum>ban)//前半段个数大于一半
            {
                summ++;
                continue;//就不用看后半段了
            }
            memset(visted,0,sizeof(visted));
            sum=0;
            for(j=0;j<hou[i].size();j++)//和判断前半段一样,复制粘贴就是qian数组改为hou数组
            {
                int p=hou[i][j];
                qu.push(p);
            }
            while(!qu.empty())
            {
                int q=qu.front();
                if(visted[q]==0)
                {
                    visted[q]=1;
                    sum++;
                }
                for(int k=0;k<hou[q].size();k++)
                {
                    if(visted[hou[q][k]]==0)qu.push(hou[q][k]);
                }
                qu.pop();
            }
            if(sum>ban)summ++;
        }
        cout<<summ<<endl;
    }
    return 0;
 } 

原文地址:https://www.cnblogs.com/ydw--/p/10982314.html

时间: 2024-10-05 05:41:30

toj 2282: Median Weight Bead (队列+vector)的相关文章

POJ 1975 Median Weight Bead

Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 197564-bit integer IO format: %lld      Java class name: Main There are N beads which of the same shape and size, but with different weights.

poj 1975 Median Weight Bead(传递闭包 Floyd)

链接:poj 1975 题意:n个珠子,给定它们之间的重量关系,按重量排序,求确定肯定不排在中间的珠子的个数 分析:因为n为奇数,中间为(n+1)/2,对于某个珠子,若有至少有(n+1)/2个珠子比它重或轻,则它肯定不排在中间 可以将能不能确定的权值初始化为0,能确定重量关系的权值设为1 #include<stdio.h> #include<string.h> int a[110][110]; int main() { int T,n,m,i,j,k,d,x,sum; scanf(

POJ1975:Median Weight Bead(FLOYD)

Description 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 foll

TOJ-1317 Median Weight Bead

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 compar

Median Weight Bead_floyd

Description 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 foll

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20

sicily 1443 队列基本操作

1443. Printer Queue Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you