题意:

一张由 n 个点,m 条边构成的有向无环图。每个点有点权 Ai。QQ 小方想知道所有起点为 1 ,终点为 n 的路径中最大的中位数是多少。

一条路径的中位数指的是:一条路径有 n 个点,将这 n 个点的权值从小到大排序后,排在位置 ⌊n2⌋+1 上的权值。

思路(官方题解):

考虑二分答案,我们需要验证路径最大的中位数是否 ≥mid 。

我们把所有的点权做 −1/1 变换,即 ≥mid 的点权变为 1 ,否则变为 −1 。

根据题面路径中位数的定义,我们可以发现,如果这条路径的中位数 ≥mid ,那么做了 −1/1 变换以后,这条路径上的点权和 ≥0 。

而我们现在需要知道的问题是路径最大的中位数是否 ≥mid ,也就是说,最大的路径点权是否 ≥0 。

跑一遍最长路就好了。而对于 DAG ,最长路只要 dp 一下,复杂度是保证 O(m) 。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<cmath>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 1e6+;
const int maxm = 1e6+;
const int inf = 0x3f3f3f3f; int a[maxn];
int b[maxn];
vector<int>v[maxn];
int dp[maxn];
int n,m;
int c(int x, int mid){
return a[x]>=mid?:-;
}
void dpp(int x, int va,int mid){
//printf("%d %d %d\n",x,va,mid);
if(va<=dp[x])return;
dp[x] = max(dp[x],va);
//if(x==n)return;
for(int i = ; i < (int)v[x].size(); i++){
dpp(v[x][i], va+c(v[x][i],mid),mid);
}
return;
}
bool ck(int x){
//x = b[x];
for(int i = ; i <= n; i++)dp[i]=-0x3f3f3f3f;
dpp(,c(,x),x); if(dp[n]>=)return true;
return false;
}
int main(){
scanf("%d %d",&n,&m);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
for(int i = ; i <= m; i++){
int x, y;
scanf("%d %d", &x, &y);
v[x].pb(y);
}
//printf("%d",ck(5));
int l = , r = 1e9;
int ans=-;
while(l<=r){
int mid = (r+l)>>;
//printf("%d %d %d\n",l,r,mid);
if(ck(mid)){
l = mid+;
ans=mid;
}
else r = mid-;
}
printf("%d", ans);
return ;
}

EOJ Monthly 2019.2 E 中位数 (二分+中位数+dag上dp)的更多相关文章

  1. 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 秒 内存限制:  ...

  2. EOJ Monthly 2019.2 E. 中位数 (二分+dfs)

    题目传送门 题意: 在一个n个点,m条边的有向无环图中,求出所有从1到n 的路径的中位数的最大值 一条路径的中位数指的是:一条路径有 n 个点, 将这 n 个点的权值从小到大排序后,排在位置 ⌊n2⌋ ...

  3. EOJ Monthly 2019.2

    题解 A 回收卫星 #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/s ...

  4. 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( ...

  5. EOJ Monthly 2019.2 A. 回收卫星

    题目传送门 题意: 你可以询问一个三维坐标,机器会告诉你这个坐标在不在目标圆中, 并且(0,0,0)是一定在圆上的,叫你求出圆心坐标 思路: 因为(0,0,0)一定在圆上,所以我们可以把圆心分成3个坐 ...

  6. EOJ Monthly 2019.2 (based on February Selection) F.方差

    题目链接: https://acm.ecnu.edu.cn/contest/140/problem/F/ 题目: 思路: 因为方差是用来评估数据的离散程度的,因此最优的m个数一定是排序后连续的,所以我 ...

  7. EOJ Monthly 2019.2 (based on February Selection) D.进制转换

    题目链接: https://acm.ecnu.edu.cn/contest/140/problem/D/ 题目: 思路: 我们知道一个数在某一个进制k下末尾零的个数x就是这个数整除kx,这题要求刚好末 ...

  8. EOJ Monthly 2019.2 (based on February Selection) D 进制转换 【数学 进制转换】

    任意门:https://acm.ecnu.edu.cn/contest/140/problem/D/ D. 进制转换 单测试点时限: 2.0 秒 内存限制: 256 MB “他觉得一个人奋斗更轻松自在 ...

  9. EOJ Monthly 2019.1 唐纳德先生与这真的是签到题吗 【数学+暴力+multiset】

    传送门:https://acm.ecnu.edu.cn/contest/126/ C. 唐纳德先生与这真的是签到题吗 单测试点时限: 6.0 秒 内存限制: 1024 MB 唐纳德先生在出月赛的过程中 ...

随机推荐

  1. Ant Design Pro中Transfer穿梭框的实际用法(与后端交互)

    Ant Design Pro中Transfer穿梭框的实际用法(与后端交互) 该控件的属性以及属性的作用在ADP的官方文档中都有介绍,但没有讲如何与后端交互,本文旨在讲解该控件与后端的交互. Ant ...

  2. npm安装报错npm ERR! Refusing to install package with name "xxxx" under a packagexxxx

    npm ERR! code ENOSELF npm ERR! Refusing to install package with name "webpack" under a pac ...

  3. C# DataTable数据类型判断

    当我们从数据中获取到数据,一般会使用 DataTable 接收,然后会遍历每行数据.由于从数据库中读取的数据可能为空,比如我们的编译代码如下: foreach (DataRow datarow in ...

  4. 原型,原型链,给予原型和class的继承

    学习react的时候遇到了class方式的继承语法,原理和代码的解释很详细,值得一读. 原型每个函数(构造函数)都有一个 prototype 属性,指向该函数(构造函数)的原型对象.实例没有 prot ...

  5. 详细解析Java虚拟机的栈帧结构

    欢迎关注微信公众号:万猫学社,每周一分享Java技术干货. 什么是栈帧? 正如大家所了解的,Java虚拟机的内存区域被划分为程序计数器.虚拟机栈.本地方法栈.堆和方法区.(什么?你还不知道,赶紧去看看 ...

  6. php5.6.39 源码安装

    1 安装依赖库 yum install -y autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel li ...

  7. codeforces 上的找两人的幸运天

    Bob and Alice are often participating in various programming competitions. Like many competitive pro ...

  8. java 魔术

    每4个字节都有对应的含义

  9. 一个按键搞定日常git操作

    Git is a free and open source distributed version control system designed to handle everything from ...

  10. 网络通信-基本概念:网络、IP地址、端口、socket

    目录 网络通信 1 网络 1.1 网络定义 1.2 使用网络的目的 1.3 总结 2 IP地址 2.1 ip地址的作用 2.2 ip地址的分类 3 端口 3.1 什么是端口 3.2 端口号 3.3 端 ...