【牛客网】Idol Master
【牛客网】Idol Master
也是一道网络流解线性规划
不过需要从小于号的那边解
限制是\(a \leq \sum_{i = 1}^{k} x_{i}\leq b\)
其中\(0 \leq x_{i} \leq 1\)
\(\sum_{i = 1}^{k} x_{i} = b - y_{i}\)
\(y_i \leq b - a\)
然后把\(N - k + 1\)个式子都列出来,前后补上0=0,就可以解了
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next,cap;int64 val;
}E[1000005];
int head[405],sumE = 1,Ncnt,S,T;
int64 ans,c[405];
int N,K,A,B;
bool vis[405];
void add(int u,int v,int c,int a) {
E[++sumE].to = v;
E[sumE].next = head[u];
E[sumE].cap = c;
E[sumE].val = a;
head[u] = sumE;
}
void addtwo(int u,int v,int c,int a) {
add(u,v,c,a);add(v,u,0,-a);
}
int64 dis[405];
bool inq[405];
queue<int> Q;
bool SPFA() {
for(int i = 1 ; i <= Ncnt ; ++i) dis[i] = -1e18;
dis[S] = 0;Q.push(S);
memset(inq,0,sizeof(inq));
while(!Q.empty()) {
int u = Q.front();Q.pop();
inq[u] = 0;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(E[i].cap) {
if(dis[u] + E[i].val > dis[v]) {
dis[v] = dis[u] + E[i].val;
if(!inq[v]) {Q.push(v);inq[v] = 1;}
}
}
}
}
return dis[T] > -1e18;
}
int dfs(int u,int aug) {
if(u == T) {
ans += aug * dis[T];
return aug;
}
vis[u] = 1;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(!vis[v] && E[i].cap && dis[v] == dis[u] + E[i].val) {
int t = dfs(v,min(E[i].cap,aug));
if(t) {
E[i].cap -= t;
E[i ^ 1].cap += t;
return t;
}
}
}
return 0;
}
void MCMF() {
while(SPFA()) {
do {
memset(vis,0,sizeof(vis));
}while(dfs(S,0x7fffffff));
}
}
void Solve() {
ans = 0;memset(head,0,sizeof(head));sumE = 1;
read(N);read(K);read(A);read(B);
for(int i = 1 ; i <= N ; ++i) {
read(c[i]);
}
int d = N - K + 2;
S = d + 1;T = d + 2;Ncnt = d + 2;
for(int i = 1 ; i <= K ; ++i) {
addtwo(min(i + 1,d),1,1,c[i]);
}
for(int i = K + 1 ; i <= N ; ++i) {
addtwo(min(i + 1,d),i - K + 1,1,c[i]);
}
for(int i = 2 ; i <= d ; ++i) {
addtwo(i,i - 1,B - A,0);
}
addtwo(1,T,B,0);
addtwo(S,d,B,0);
MCMF();
out(ans);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int T;
read(T);
for(int i = 1 ; i <= T ; ++i) Solve();
return 0;
}
【牛客网】Idol Master的更多相关文章
- 牛客网 --java问答题
http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...
- 牛客网《BAT面试算法精品课》学习笔记
目录 牛客网<BAT面试算法精品课>学习笔记 牛客网<BAT面试算法精品课>笔记一:排序 牛客网<BAT面试算法精品课>笔记二:字符串 牛客网<BAT面试算法 ...
- C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...
- 牛客网第9场多校E(思维求期望)
链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- Beautiful Numbers(牛客网)
链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...
- 牛客网华为机试题之Python解法
牛客网华为机试题之Python解法 第1题 字符串最后一个单词的长度 a = input().split(" ") print(len(a[-1])) 第2题 计算字符个数 a = ...
- 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)
链接:https://www.nowcoder.com/acm/contest/197/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- 牛客网 2018年东北农业大学春季校赛 L题 wyh的天鹅
链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 262144K,其他语言524288 ...
随机推荐
- pandas记录
pandas的map方法使用 import pandas as pd import numpy as np data = pd.DataFrame(np.random.randint(0, 10, ( ...
- 使用Ajax和一般处理程序实现文件上传与下载
1.使用HTML的input标签 <input type="file" multiple="multiple" id="file_load&qu ...
- @Autowired 与@Resource的区别详解
spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- postgresql 创建索引:ERROR: operator class "gin_trgm_ops" does not exist for access method "gin"
g_trgm is an extension, so: CREATE EXTENSION pg_trgm; If you get the following error ERROR: could no ...
- (转)centos7安装telnet服务
借鉴:https://www.cnblogs.com/daipenglin/p/4934572.html 阅读目录 1 CentOS7.0 telnet-server 启动的问题 场景:在进行Teln ...
- hbase单点安装
系统环境:centos 6 软件包: hbase版本:hbase-1.4.8-bin.tar.gz 下载地址:wget http://mirror.bit.edu.cn/apache/hba ...
- 探究机器码,深入研究C语言程序的机制
用C高级编程语言写的程序,都要经过编译链接成本机可执行的程序.这个可执行程序究竟是什么呢?在Linux上,我们可以用objdump命令很方便地查看一个可执行程序的机器码.我们从一个简单的示例开始,说一 ...
- CSS Pixel 和 Device pixels
Web developers need CSS pixels, that is, the pixels that are used in CSS declarations such as " ...
- Leetcode题 112 和 113. Path Sum I and II
112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 记一次ArrayList产生的线上OOM问题
前言:本以为(OutOfMemoryError)OOM问题会离我们很远,但在一次生产上线灰度的过程中就出现了Java.Lang.OutOfMemoryError:Java heap space异常,通 ...