EOJ Monthly 2019.2 E. 中位数 (二分+dfs)
题意:
在一个n个点,m条边的有向无环图中,求出所有从1到n
的路径的中位数的最大值
一条路径的中位数指的是:一条路径有 n 个点,
将这 n 个点的权值从小到大排序后,排在位置 ⌊n2⌋+1 上的权值。
思路:
看到权值为1~1e9,可以想到用二分答案,然后我们在验证的时候
可以将小于mid的边权设为-1,大于为1这样遍历一遍序列加起来的值
刚好为0
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 1000005
const ll INF=2e9;
int n,m;
int a[N];
vector<int>v[N];
int dis[N];
int vis[N]; int dfs(int mid,int x)
{
if(vis[x]) return dis[x];
int tmp=a[x]>=mid?:-;
vis[x]=;
for(int i=;i<v[x].size();i++)
{
int t=v[x][i];
dis[x]=max(dis[x],tmp+dfs(mid,t));
}
return dis[x];
}
bool check(int mid)
{
for(int i=;i<=n;i++) dis[i]=-INF;
memset(vis,,sizeof(vis));
vis[]=;
dis[]=a[]>=mid?:-;
return dfs(mid,n)>=;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=;i<=n;i++) scanf("%d",&a[i]);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
v[y].push_back(x);
}
ll l=,r=INF;
ll ans=-;
while(l<=r)
{
ll mid=l+r>>;
if(check(mid))
{
l=mid+;
ans=mid;
}
else r=mid-;
}
printf("%lld\n",ans);
} return ;
}
EOJ Monthly 2019.2 E. 中位数 (二分+dfs)的更多相关文章
- EOJ Monthly 2019.2 E 中位数 (二分+中位数+dag上dp)
题意: 一张由 n 个点,m 条边构成的有向无环图.每个点有点权 Ai.QQ 小方想知道所有起点为 1 ,终点为 n 的路径中最大的中位数是多少. 一条路径的中位数指的是:一条路径有 n 个点,将这 ...
- EOJ Monthly 2019.2 题解(B、D、F)
EOJ Monthly 2019.2 题解(B.D.F) 官方题解:https://acm.ecnu.edu.cn/blog/entry/320/ B. 解题 单测试点时限: 2.0 秒 内存限制: ...
- EOJ Monthly 2019.2
题解 A 回收卫星 #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/s ...
- EOJ Monthly 2019.11 E. 数学题(莫比乌斯反演+杜教筛+拉格朗日插值)
传送门 题意: 统计\(k\)元组个数\((a_1,a_2,\cdots,a_n),1\leq a_i\leq n\)使得\(gcd(a_1,a_2,\cdots,a_k,n)=1\). 定义\(f( ...
- EOJ Monthly 2019.2 A. 回收卫星
题目传送门 题意: 你可以询问一个三维坐标,机器会告诉你这个坐标在不在目标圆中, 并且(0,0,0)是一定在圆上的,叫你求出圆心坐标 思路: 因为(0,0,0)一定在圆上,所以我们可以把圆心分成3个坐 ...
- EOJ Monthly 2019.2 (based on February Selection) F.方差
题目链接: https://acm.ecnu.edu.cn/contest/140/problem/F/ 题目: 思路: 因为方差是用来评估数据的离散程度的,因此最优的m个数一定是排序后连续的,所以我 ...
- EOJ Monthly 2019.2 (based on February Selection) D.进制转换
题目链接: https://acm.ecnu.edu.cn/contest/140/problem/D/ 题目: 思路: 我们知道一个数在某一个进制k下末尾零的个数x就是这个数整除kx,这题要求刚好末 ...
- EOJ Monthly 2019.2 (based on February Selection) D 进制转换 【数学 进制转换】
任意门:https://acm.ecnu.edu.cn/contest/140/problem/D/ D. 进制转换 单测试点时限: 2.0 秒 内存限制: 256 MB “他觉得一个人奋斗更轻松自在 ...
- EOJ Monthly 2019.1 唐纳德先生与这真的是签到题吗 【数学+暴力+multiset】
传送门:https://acm.ecnu.edu.cn/contest/126/ C. 唐纳德先生与这真的是签到题吗 单测试点时限: 6.0 秒 内存限制: 1024 MB 唐纳德先生在出月赛的过程中 ...
随机推荐
- ESP8266的使用学习
ESP8266-01 ESP8266-12F简介 让灯闪烁 ESP8266-中断 模拟输入(ADC-A0) 模拟输出(PWM) 串口通信(Serial) EEPROM类库的使用 ...
- 关于softmax稳定性问题
因为softmax中指数函数,很容易超出计算机表达的最大值,所以采用分子分母同时乘N的方法,N一般为最大值.
- Mybatis 并发执行导致cpu占满的问题
最近线上服务经常 出现cpu达到100%的问题,发现都是执行oracle操作的方法就没有返回.经过排查,最后定位到cpu消耗在以下方法 System.Collections.Generic.Dicti ...
- React Native 之 createBottomTabNavigator,createMaterialTopTabNavigator
icon第三方库 yarn add react-native-vector-icons react-native link react-native-vector-icons 在上次的代码中添加: A ...
- Spring boot @Transactional
1.注解@Transactional 2.异常回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); @Ov ...
- CF G. Orientation of Edges BFS
来两遍 $BFS,$ 都贪心一下即可. #include <bits/stdc++.h> #define maxn 300009 using namespace std; void set ...
- luogu 2219[HAOI2007]修筑绿化带 单调队列
Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in",& ...
- A - Race to 1 Again
题目 Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be di ...
- 解决 UIAlterController 不居中问题
最后更新:2017-06-30 现象描述 新公司做的解决的第一个bug 就是 UIAlterController 不居中,莫名其妙的飞出屏幕之外 找了很久的答案,最终在苹果论坛看到了相关的描述 We ...
- POJ 1432 Decoding Morse Sequences (DP)
Decoding Morse Sequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/129783#problem/D Description Be ...