第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)
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.
In this problem, the exact value of each element is not given, but relations between some pair of elements are given. The -th relation can be described as , which indicates that the -th element is strictly larger than the -th element.
For all , is it possible to assign values to each element so that all the relations are satisfied and the -th element is the median of the elements?
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers and (, ), indicating the number of elements and the number of relations. It’s guaranteed that is odd.
For the following lines, the -th line contains two integers and , indicating that the -th element is strictly larger than the -th element. It guaranteed that for all , or .
It’s guaranteed that the sum of of all test cases will not exceed .
Output
For each test case output one line containing one string of length . If it is possible to assign values to each element so that all the relations are satisfied and the -th element is the median, the -th character of the string should be ‘1’, otherwise it should be ‘0’.
Sample Input
2
5 4
1 2
3 2
2 4
2 5
3 2
1 1
2 3
Sample Output
01000 000
Hint
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
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int dp[105][105],in[105],out[105];
int init()
{
for(int i=1;i<=105;i++)
{
in[i]=0;
out[i]=0;
for(int j=1;j<=100;j++)
{
dp[i][j]=0;
}
}
}
int main()
{
long long t;
cin>>t;
while(t--)
{
init();
long long n,m;
cin>>n>>m;
long long flag=0;
for(int i=1;i<=m;i++)
{
long long l,r;
cin>>l>>r;
dp[l][r]=1;
if(l==r) flag=1; //自己排在自己前面是不可能的,直接按题意输出0
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dp[i][j]=(dp[i][k]&&dp[k][j]);//floyd 讨论图的连通性
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dp[i][j]==1&&dp[j][i]==1)
flag=1;
//floyd 讨论图的连通性后,如果出现环的话,就是我排在你前面。你排在我前面,同样不可能
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(dp[i][j]==1)
{
in[i]++; //有几个人排在第I个人前面
out[j]++;//有几个人排在第j个人后面面
}
if(flag) //不符合现实的按题意输出0
{
for(int i=1;i<=n;i++)cout<<"0";
cout<<endl;
}
else
{
for(int i=1;i<=n;i++)
{
if(in[i]>=(n+1)/2||out[i]>=(n+1)/2) cout<<"0";
//如果在他前面或者在他后面的不等于一般的人数,他绝对不是中间位置
else cout<<"1";
}
cout<<endl;
}
}
return 0;
}
Median Weight Bead
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4160 Accepted: 2154
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 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.
Input
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.
Output
There should be one line per test case. Print the number of beads which can never have the medium weight.
Sample Input
1
5 4
2 1
4 3
5 1
4 2
Sample Output
2
Source
Tehran Sharif 2004 Preliminary
#include<cstdio>
#include<cstring>
using namespac std;
int a[110][110];
int main()
{
int T,n,m,i,j,k,d,x,sum;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
while(m--){
scanf("%d%d",&i,&j);
a[i][j]=1;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][k]&&a[k][j]) //当i比k重,k比j重,则i比j重
a[i][j]=1;
sum=0;
for(i=1;i<=n;i++){
d=x=0;
for(j=1;j<=n;j++){
if(a[i][j]) 跟前面的联通++
d++;
else if(a[j][i]) 跟后面的联通++
x++;
}
if(d>=(n+1)/2||x>=(n+1)/2) 如果他是中间位置,他对于前面的连通性应该等于后面,如果不等于且确定的话,他一定不是中间位置。
sum++;
}
printf("%d\n",sum);
}
return 0;
}
第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)的更多相关文章
- Triangle (第8届山东省赛的某题)
triangle(第8届山东省赛的某题) 传送门 题意:喵了个呜,这题意真是峰回路转啊.懒死了,不想描述. 做法:我们拿set或线段树维护exp的最小值,每次取出exp值最小的边,删除之.并更新这条边 ...
- 2013年山东省赛F题 Mountain Subsequences
2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...
- HEX SDUT 3896 17年山东省赛D题
HEX SDUT 3896 17年山东省赛D题这个题是从矩形的左下角走到右上角的方案数的变形题,看来我对以前做过的题理解还不是太深,或者是忘了.对于这种题目,直接分析它的性质就完事了.从(1,1)走到 ...
- 河南省第十届省赛 Intelligent Parking Building
title: Intelligent Parking Building 河南省第十届省赛 tags: [模拟,省赛] 题目描述: There is a new revolution in the pa ...
- 河南省第十届省赛 Plumbing the depth of lake (模拟)
title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...
- 四川第十届省赛 A.Angel Beats bitset
四川第十届省赛 A.Angel Beats bitset 题目链接 题解参考:http://www.cnblogs.com/Aragaki/p/9142250.html 考虑用bitset来维护对于所 ...
- 2019第十届蓝桥杯 E题 迷宫
/*输入 30 50 01010101001011001001010110010110100100001000101010 00001000100000101010010000100000001001 ...
- 第十届山东省acm省赛补题(2)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second ...
- 第十届山东省acm省赛补题(1)
今天第一场个人训练赛的题目有点恐怖啊,我看了半个小时多硬是一道都不会写.我干脆就直接补题去了.... 先补的都是简单题,难题等我这周末慢慢来吧... A Calandar Time Limit: 1 ...
随机推荐
- css进阶选择器
后代选择器 用空格隔开 选择div标签下的p标签下的a标签 div p a 选择class为parent标签下的p标签下的a标签 .parent p a 后代选择器可以是标签.类.id的混合体 后代选 ...
- (js描述的)数据结构[字典](7)
(js描述的)数据结构[字典](7) 一.字典的特点 1.字典的主要特点是一一对应关系. 2.使用字典,剋通过key取出对应的value值. 3.字典中的key是不允许重复的,而value值是可以重复 ...
- django 利用ORM对单表进行增删改查
牛小妹上周末,一直在尝试如何把数据库的数据弄到界面上.毕竟是新手,搞不出来,文档也看不懂.不过没关系,才刚上大学.今晚我们就来解释下,要把数据搞到界面的第一步.先把数据放到库里,然后再把数据从库里拿出 ...
- MYSQ创建联合索引,字段的先后顺序,对查询的影响分析
MYSQ创建联合索引,字段的先后顺序,对查询的影响分析 前言 最左匹配原则 为什么会有最左前缀呢? 联合索引的存储结构 联合索引字段的先后顺序 b+树可以存储的数据条数 总结 参考 MYSQ创建联合索 ...
- Netty:ChannelFuture
上一篇我们完成了对Channel的学习,这一篇让我们来学习一下ChannelFuture. ChannelFuture的简介 ChannelFuture是Channel异步IO操作的结果. Netty ...
- mysql 不能对同一个表进行 update(delete) 和 select 联合操作
eq: update a set a.x = 1 where a.y in (select a.x from a); 上边语法是错误的,在对aupdate 时不能再条件中对同一个a表进 ...
- C++常用注意事项
new和delete:现在还没有new[][]和delete[][],所以在用这些的时候最好用循环解决,先一个指针的数组,然后再初始化,每个元素再new一下,这样就满足了多维数组的条件:比如: int ...
- 在Python中该如何实现Java的重写与重载
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:清风python PS:如有需要Python学习资料的小伙伴可以加点击 ...
- python调用小豆机器人实现自己的机器人!
大家好,人工智能是不是很酷呢? 今天我们用python调用小豆机器人实现自己的机器人(可以结合往期的语音识别更酷哦) 好,废话不多说直接上代码 import requests i=input(&quo ...
- PHP函数:debug_backtrace
debug_backtrace() - 产生一条 PHP 的回溯跟踪(backtrace). 说明: debug_backtrace ([ int $options = DEBUG_BACKTRAC ...