Median题目讲解

Median

题目

Recall the definition of the median of nelements where n n n is odd: sort these elements and the median is the ( n + 1 ) 2 \frac{(n + 1)}{2} 2(n+1) -th largest element.

In this problem, the exact value of each element is not given, but m relations between some pair of elements are given. The i − t h i-th ith relation can be described as ( a i , b i ) (a_i,b_i) (ai,bi), which indicates that the a i − t h a_i-th aith element is strictly larger than the b i − t h b_i -th bith element.

For all 1 ≤ k ≤ n, is it possible to assign values to each element so that all the relations are satisfied and the k − t h k-th kth element is the median of the n elements?

输入

There are multiple test cases. The first line of the input contains an integer T T T, indicating the number of test cases. For each test case:
The first line contains two integers n n nand m m m ( 1 ≤ n < 100 , 1 ≤ m ≤ n 2 ) (1≤n<100, 1≤m≤n^2) (1n<100,1mn2), indicating the number of elements and the number of relations. It’s guaranteed that n n n is odd.

For the following m m mlines, the i − t h i-th ith line contains two integers a i a_i aiand b i b_i bi, indicating that the a i − t h ai-th aith element is strictly larger than the b i − t h b_i -th bith element. It guaranteed that for all 1 ≤ i < j ≤ m , a i ≠ a j 1≤i<j≤m, a_i≠a_j 1i<jm,ai=aj or b i ≠ b j b_i≠b_j bi=bj

It’s guaranteed that the sum of n of all test cases will not exceed 2 × 1 0 3 2 × 10 ^3 2×103

输出

For each test case output one line containing one string of length n. If it is possible to assign values to each element so that all the relations are satisfied and the i − t h i-th ith element is the median, the i − t h i-th ith character of the string should be ′ 1 ′ '1' 1, otherwise it should be ′ 0 ′ '0' 0.

Examples

Input

2
5 4
1 2
3 2
2 4
2 5
3 2
1 1
2 3

Outout

01000
000

Note

For the first sample test case, as the 2nd element is smaller than the 1st and the 3rd elements and is larger than the 4th and the 5th elements, it’s possible that the 2nd element is the median.

For the second sample test case, as the 1st element can’t be larger than itself, it’s impossible to assign values to the elements so that all the relations are satisfied.

题目汉化

给你 T T T组测试,每组测试 n n n个数字 m m m个大小关系,接下来是 m m m条对应关系,前面的大于后面的,就是比如输入:3 2,意思是第3个位置大于第2个位置,题目让你找到他们的中位数(中位数就是排序后最中间的那个数字),中位数的那一位输出1,其他输出0。

做法

参考佬的做法,hh
https://blog.csdn.net/weixin_45675097/article/details/120917027
因为每个数之间都有一定的关系,比如第5位大于第4位,第4位大于第3位,那么就隐含了一组对应关系,就是第5位大于第3位
所以求得每个数字的对应关系后就很好得到了

每个数字的相应关系,很容易想到Floyd算法

  • int mp[N][N]记录每个数字的对应关系,如上面的例子就是mp[5][4] = 1,意思就是5比4这个位置的数字大
 while (m--)
 {
      int l, r;
      cin >> l >> r;
      mp[l][r] = 1;
      if (l == r)
          ok = 1;
}
  • 这个ok就是看合法不合法,不能存在一个数字比自己大吧,hh~~

  • 然后求每个数对应每个数的关系,

//不能第5位 比 第4位 大,第4位 又比第 5位 大吧
 if (mp[i][j] && mp[j][i])
{
	ok = 1;
	break;
}
else
{
	//如果 第5位 比 第4位 大, 第4位 比第3位 大,那么就是mp[5][3] |= mp[5][4]&mp[4][3]
	//如果mp[5][3]已经是1了, 所以就是 用 或 |= ,
	
	mp[i][j] |= mp[i][k] & mp[k][j];
}
                
  • 如果 ok 成立,就是不合法,直接输出结果就是n个0
  • 合法的话,就看入度和出度,中位数的入度 din < = <= <= n 2 \frac{n}{2} 2n,出度dout也是 < = <= <= n 2 \frac{n}{2} 2n,对吧
  • 所以遍历一遍,复合条件输出1,不然输出0

代码

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define int long long
#define endl '\n'
const int N = 2e5 + 5;
const int INF = 2e9 + 1;
typedef pair<int, int> PII;

int mp[105][105];
int din[105], dout[105];
void solve()
{
    int n, m;
    cin >> n >> m;
    bool ok = 0;
    memset(mp, 0, sizeof mp);
    while (m--)
    {
        int l, r;
        cin >> l >> r;
        mp[l][r] = 1;
        if (l == r)
            ok = 1;
    }
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (mp[i][j] && mp[j][i])
                {
                    ok = 1;
                    break;
                }
                else
                {
                    mp[i][j] |= mp[i][k] & mp[k][j];
                }

    if (ok)
    {
        for (int i = 1; i <= n; i++)
            cout << 0;
        cout << endl;
        return;
    }
    memset(din, 0, sizeof din);
    memset(dout, 0, sizeof dout);

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i != j && mp[i][j])
            {
                din[j]++;
                dout[i]++;
            }
    for (int i = 1; i <= n; i++)
        if (din[i] <= n / 2 && dout[i] <= n / 2)
            cout << 1;
        else
            cout << 0;
    cout << endl;
}

signed main()
{
    IOS;
    int T;
    cin >> T;
    while (T--)
        solve();

    return 0;
}
  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值