Solution -「多校联训」数学考试
\(\mathcal{Description}\)
Link.
给定 \(n\) 个函数,第 \(i\) 个有 \(f_i(x)=a_ix^3+b_ix^2+cx_i+d~(x\in[l_i,r_i]\cap\mathbb Z)\),还有 \(m\) 条形如 \(x_u\le x_v+d\) 的限制,请最大化 \(\sum_{i=1}^nf_i(x_i)\) 或声明无解。
\(n,|l_i|,|r_i|\le 100\)。
\(\mathcal{Solution}\)
很久没遇到了,压根儿没往网络流方面想 qwq。
对于每个 \(f_i\),拉一条代表 \(f_i(l_i..r_i)\) 的链,边权就是某个 \(f\) 的值的相反数;限制条件方便转化为最小割,之后直接跑最小割即可。
\(\mathcal O(\operatorname{Dinic}(\sum(r-l),m\sum(r-l)))\)。
\(\mathcal{Code}\)
/*~Rainybunny~*/
#include <queue>
#include <cstdio>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
#define int long long
inline int imin( const int a, const int b ) { return a < b ? a : b; }
const int MAXN = 100, IINF = 1ll << 50, BASE = 7e6;
int n, m, lid[MAXN + 5];
struct Function {
int a, b, c, d, l, r;
inline void read() {
scanf( "%lld %lld %lld %lld %lld %lld" , &a, &b, &c, &d, &l, &r );
}
inline int operator () ( const int x ) const {
return d + x * ( c + x * ( b + x * a ) );
}
} fc[MAXN + 5];
struct FlowGraph {
static const int MAXND = 2e4 + 10, MAXEG = 2e5;
int ecnt, bound, S, T, head[MAXND];
struct Edge { int to, flw, nxt; } graph[MAXEG * 2];
int ds[MAXND], curh[MAXND];
FlowGraph(): ecnt( 1 ) {}
inline void clear() {
ecnt = 1;
rep ( i, 0, bound ) head[i] = 0;
}
inline void operator () ( const int s, const int t, const int f ) {
graph[++ecnt] = { t, f, head[s] }, head[s] = ecnt;
graph[++ecnt] = { s, 0, head[t] }, head[t] = ecnt;
}
inline bool bfs() {
static std::queue<int> que;
rep ( i, 0, bound ) ds[i] = IINF;
que.push( S ), ds[S] = 0;
while ( !que.empty() ) {
int u = que.front(); que.pop();
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( graph[i].flw && ds[u] + 1 < ds[v = graph[i].to] ) {
ds[v] = ds[u] + 1, que.push( v );
}
}
}
return ds[T] != IINF;
}
inline int dfs( const int u, int iflw ) {
if ( u == T ) return iflw;
int oflw = 0;
for ( int& i = curh[u], v; i; i = graph[i].nxt ) {
if ( graph[i].flw && ds[u] + 1 == ds[v = graph[i].to] ) {
int tmp = dfs( v, imin( iflw - oflw, graph[i].flw ) );
oflw += tmp, graph[i].flw -= tmp, graph[i ^ 1].flw += tmp;
if ( iflw == oflw ) break;
}
}
if ( !oflw ) ds[u] = IINF;
return oflw;
}
inline int calc( const int s, const int t ) {
int ret = 0; S = s, T = t;
while ( bfs() ) {
rep ( i, 0, bound ) curh[i] = head[i];
ret += dfs( S, IINF );
}
return ret;
}
} G;
signed main() {
freopen( "sleep.in", "r", stdin );
freopen( "sleep.out", "w", stdout );
int Q; scanf( "%lld", &Q );
while ( Q-- ) {
scanf( "%lld %lld", &n, &m ), G.clear();
int S = 0, T = 1, node = 1;
rep ( i, 1, n ) {
fc[i].read();
G( S, lid[i] = ++node, IINF );
rep ( j, fc[i].l, fc[i].r ) {
G( node, node + 1, BASE - fc[i]( j ) ), ++node;
}
G( node, T, IINF );
}
rep ( i, 1, m ) {
int u, v, d; scanf( "%lld %lld %lld", &u, &v, &d );
rep ( x, fc[u].l, fc[u].r ) {
if ( fc[v].l <= x - d && x - d <= fc[v].r ) {
G( lid[u] + x - fc[u].l, lid[v] + x - d - fc[v].l, IINF );
} else if ( x - d > fc[v].r ) {
G( lid[u] + x - fc[u].l, T, IINF );
}
}
}
G.bound = node;
int ans = -G.calc( S, T );
if ( ans <= -IINF ) puts( "mei ji ge" );
else printf( "%lld\n", ans + n * BASE );
}
return 0;
}
Solution -「多校联训」数学考试的更多相关文章
- Solution -「多校联训」排水系统
\(\mathcal{Description}\) Link. 在 NOIP 2020 A 的基础上,每条边赋权值 \(a_i\),随机恰好一条边断掉,第 \(i\) 条段的概率正比于 \(a ...
- Solution -「多校联训」I Love Random
\(\mathcal{Description}\) 给定排列 \(\{p_n\}\),可以在其上进行若干次操作,每次选取 \([l,r]\),把其中所有元素变为原区间最小值,求能够得到的所有不同序 ...
- Solution -「多校联训」签到题
\(\mathcal{Description}\) Link. 给定二分图 \(G=(X\cup Y,E)\),求对于边的一个染色 \(f:E\rightarrow\{1,2,\dots,c\ ...
- Solution -「多校联训」朝鲜时蔬
\(\mathcal{Description}\) Link. 破案了,朝鲜时蔬 = 超现实树!(指写得像那什么一样的题面. 对于整数集 \(X\),定义其 好子集 为满足 \(Y\sub ...
- Solution -「多校联训」消失的运算符
\(\mathcal{Description}\) Link. 给定长度为 \(n\) 的合法表达式序列 \(s\),其中数字仅有一位正数,运算符仅有 - 作为占位.求将其中恰好 \(k\) ...
- Solution -「多校联训」假人
\(\mathcal{Description}\) Link. 一种物品有 长度 和 权值 两种属性,现给定 \(n\) 组物品,第 \(i\) 组有 \(k_i\) 个,分别为 \((1,a ...
- Solution -「多校联训」古老的序列问题
\(\mathcal{Description}\) Link. 给定序列 \(\{a_n\}\),和 \(q\) 次形如 \([L,R]\) 的询问,每次回答 \[\sum_{[l,r]\su ...
- Solution -「多校联训」Sample
\(\mathcal{Description}\) Link (稍作简化:)对于变量 \(p_{1..n}\),满足 \(p_i\in[0,1],~\sum p_i=1\) 时,求 \(\ma ...
- Solution -「多校联训」光影交错
\(\mathcal{Description}\) Link. 一个游戏包含若干次卡牌抽取,每次以 \(p_l\) 的概率得到 \(+1\),\(p_d\) 的概率得到 \(-1\),否则得到 ...
随机推荐
- 总结 sql 的 并集、交集、差集
有两个表 ,表a ,表b , create table a { age int , name varchar(20) } ending=innodb; insert into a values(13 ...
- spring cloud Zuul 多层拦截 --- 心得
1.前言 根据教材.博客文章的实例实操,基本都是单层拦截,没有找到多层拦截的具体写法 ,让我走了很多弯路,我将其写在这里,以待以后参考. 2.环境 spring boot : 2.1.6.RELEAS ...
- JDBC 处理sql查询多个不确定参数
JDBC程序,为了防止SQL注入,通常需要进行参数化查询,但是如果存在多个不确定参数,就比较麻烦了,查阅了一些资料,最后解决了这个问题,现在这里记录一下: public List<TabDl ...
- 学习Flutter从0开始
一. 认识Flutter 1.1. 什么是Flutter 先看看官方的解释: Flutter is Google's UI toolkit for building beautiful, native ...
- kafka学习笔记(一)消息队列和kafka入门
概述 学习和使用kafka不知不觉已经将近5年了,觉得应该总结整理一下之前的知识更好,所以决定写一系列kafka学习笔记,在总结的基础上希望自己的知识更上一层楼.写的不对的地方请大家不吝指正,感激万分 ...
- 论文解读DEC《Unsupervised Deep Embedding for Clustering Analysis》
Junyuan Xie, Ross B. Girshick, Ali Farhadi2015, ICML1243 Citations, 45 ReferencesCode:DownloadPaper: ...
- 使用NSSM把任意exe程序注册成Windows服务
1.登录NSSM官网下载最新版本: http://www.nssm.cc/download 2.解压之后可以看到win32和win64位的nssm.exe,根据自己的电脑选择使用 3.拷贝一份nssm ...
- CMake与OpenMP
CMake与OpenMP cmake_minimum_required (VERSION 2.6) project (TEST) set (TEST_VERSION 0.1) set(CMAKE_BU ...
- azure django bug
azure web app service azure web app service无法部署dhango网站 本地服务器测试代码 实际azure测试 django service 没有部署选项,需要 ...
- P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two
// Problem: P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two // Contest: Luogu // URL: https://www.luogu.com ...