L - Median(ZOJ 4124)

Time Limit : 1 Second      Memory Limit : 65536 KB

Source : 第十届山东省ACM省赛

Problem Link : ZOJ 4124

Author : Houge  Date : 2019-5-19

题目大意:

  给你n个数(不知道谁大谁小)和m个关系(ai,bi),每个关系代表ai严格大于bi。问你能否通过已知推出可能的中间项k的位置。(题目保证n一定是奇数)

分析:

  比赛时想用拓扑排序,结果因为菜没写出来,赛后听了学长用floyd的思路,感觉还不错便敲了一发。(不了解floyd的可先百度学习一下,不难)

  ·我们定义两个二维数组matrix[i][j]和num[2][n],其中matrix[i][j]=1时代表第i个数严格大于第j个数,num[0][n]代表比n小的数的个数,num[1][n]代表比n大的数的个数。

  ·跑一遍floyd,对于每一个通路,都可以认为它的起点严格大于终点,即给matrix[start][end]赋值为1。

  ·考虑一下不可能存在的情况(如样例2的第一个数严格大于自身),即图中出现自环。

  ·再对matrix进行遍历,更新num数组。

  ·最后按要求输出即可。

代码:

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 int n,m,matrix[105][105],num[2][105],flag=0;
 6
 7 void floyd()
 8 {
 9     int i,j,k;
10
11     for(k=1;k<=n;k++)      //floyd
12         for(i=1;i<=n;i++)
13             for(j=1;j<=n;j++)
14                 if(matrix[i][k]&&matrix[k][j])
15                     matrix[i][j]=1;
16
17     for(i=1;i<=n;i++)      //判断是否有自环
18         for(j=1;j<=n;j++)
19             if(matrix[i][j]&&matrix[j][i])
20             {
21                 flag=1;
22                 return;
23             }
24
25     for(i=1;i<=n;i++)    //维护更新num数组
26         for(j=1;j<=n;j++)
27             if(matrix[i][j])
28             {
29                 num[0][i]++;
30                 num[1][j]++;
31             }
32 }
33
34 int main()
35 {
36     int t;
37     scanf("%d",&t);
38     while(t--)
39     {
40         int i,a,b;
41
42         memset(matrix,0,sizeof(matrix));    //初始化
43         memset(num,0,sizeof(num));
44         flag=0;
45
46         scanf("%d%d",&n,&m);
47         for(i=0;i<m;i++)    //预处理
48         {
49             scanf("%d%d",&a,&b);
50             matrix[a][b]=1;
51         }
52         floyd();
53         if(flag==0)    //输出,当一个数比它大的和比它小的数的个数都小于等于n/2时,可视该项为中间项
54         {
55             for(i=1;i<=n;i++)
56             {
57                 if(num[0][i]<=n/2&&num[1][i]<=n/2) printf("1");
58                 else printf("0");
59             }
60             printf("\n");
61         }
62         else
63         {
64             for(i=0;i<n;i++) printf("0");
65             printf("\n");
66         }
67     }
68     return 0;
69 }

原文地址:https://www.cnblogs.com/CSGOBESTGAMEEVER/p/10890595.html

时间: 2025-01-17 09:20:27

L - Median(ZOJ 4124)的相关文章

第十届山东省acm省赛补题(2)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second      Memory Limit: 65536 KB Recall the definition of the median of  elements where  is odd: sort these elements and the median is the -th largest element.

1057. Stack (30) - 树状数组

题目例如以下: Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are

python_如何将多个小字符拼接成大字符?

案例: 下面有一个列表,如何把这个列表拼接成一个字符串? l = [1, 2, 3, 4, 'a', 'b', 'c'] 有哪些方法? 方法1: for进行迭代拼接 #!/usr/bin/python3 def go_str(l): median = '' for i in l: # 判断是否是字符串类型 if isinstance(i, str): median += i # 如果是非字符串,转换成字符串 else: i = str(i) median += i return median i

第十届山东省ACM省赛题解

点击跳转 A - Calandar H - Stones in the Bucket L - Median 原文地址:https://www.cnblogs.com/CSGOBESTGAMEEVER/p/10891007.html

The 10th Shandong Provincial Collegiate Programming Contest(8/13)

\[The\ 10th\ Shandong\ Provincial\ Collegiate\ Programming\ Contest\] \(A.Calandar\) 签到 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<bits/stdc++.h> using namespace std; function<void(void)> ____ = [](){ios_base:

百度好几个市房管局开始开发了开始

http://www.ebay.com/cln/yi_ch32/book/167329925017/20150205 http://www.ebay.com/cln/gozh754/book/167427188010/20150205 http://www.ebay.com/cln/guamyz-peuhck/book/167535329016/20150205 http://www.ebay.com/cln/nito098/book/167427189010/20150205 http://w

zoj 3612 Median (splay)

题目大意: 添加和删除一个数,然后输出中位数. 简单的Splay   维护Splay上有多少个节点就可以了 #include <cstdio> #include <iostream> #define inf 1LL<<60 #define maxn 222222 #define keyTree (ch[ch[root][1]][0]) using namespace std; typedef long long LL; int S[maxn],que[maxn],ch[

ZOJ 3612 Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them if m is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fi

zoj Median (multiset)

Median Time Limit: 5 Seconds      Memory Limit: 65536 KB The median of m numbers is after sorting them in order, the middle one number of them ifm is even or the average number of the middle 2 numbers if m is odd. You have an empty number list at fir