描述
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.
- Bead 2 is heavier than Bead 1.
- Bead 4 is heavier than Bead 3.
- Bead 5 is heavier than Bead 1.
- 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