牛客网暑期ACM多校训练营(第二场)B discount
链接:https://www.nowcoder.com/acm/contest/140/B
来源:牛客网
题目描述
There are n kinds of drinks, and the price of i-th drink is p[i] yuan per bottle.
Since White Cloud is a good friend of White Rabbit, when White Rabbit buys a bottle of i-th drink, White Rabbit can choose only one of the following two discounts :
1.White Rabbit can get a d[i](d[i]<=p[i]) yuan discount. Specifically, White Rabbit only need to pay p[i]-d[i] yuan.
2.White Rabbit can buy a bottle of f[i]-th drink for free(than bonus drink can't use any discount).
White Rabbit wants to have at least a bottle of i-th drink for each i between 1 to n. You need to tell White Rabbit what is the minimal cost.
输入描述:
The first line of input contains an integer n(n<=100000)
In the next line,there are n integers p[1..n] in range [0,1000000000].
In the next line,there are n integers d[1..n] in range [0,1000000000].(d[i]<=p[i])
In the next line,there are n integers f[1..n] in range [1,n].
输出描述:
Print the minimum cost.
输出
8 分析:考虑被赠送的商品->商品,这些商品的赠送关系就形成了基环树森林;
不考虑环,环外树形dp,dp[i][0]表示购买i的子树最小代价,dp[i][1]表示购买i的子树且i以原价购买(考虑到对父亲的赠送);
那么dp[i][1]可以直接由儿子的dp[j][0]和自身的原价更新到;
dp[i][0]有两种情况,自身被赠送得来或不赠送得来而已;
考虑环上,需要断环为链进行dp,记为g[i][0]和g[i][1],其中g[i]与dp[i]同理;
需要注意的是环上的第一件商品要分是否由最后一件商品赠送而来;
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+,mod=1e9+,inf=0x3f3f3f3f;
int n,m,k,t,p[maxn],d[maxn],pr[maxn],cir[maxn],tot,vis[maxn];
long long dp[maxn][],g[maxn][];
bool iscir[maxn];
vector<int>e[maxn];
void dfs(int x)
{
if(vis[x]==)
{
int pos=x;
while()
{
cir[++tot]=pos;
iscir[pos]=true;
pos=pr[pos];
if(pos==x)return;
}
}else if(vis[x]==)return;
vis[x]=;
for(auto y:e[x])pr[y]=x,dfs(y);
vis[x]=;
}
void dfs1(int x)
{
dp[x][]=p[x]-d[x];
dp[x][]=p[x];
long long cnt1=;
long long cnt2=1e18;
for(auto y:e[x])
{
if(iscir[y])continue;
dfs1(y);
dp[x][]+=dp[y][];
dp[x][]+=dp[y][];
}
for(auto y:e[x])
{
if(iscir[y])continue;
dp[x][]=min(dp[x][],dp[x][]-p[x]-dp[y][]+dp[y][]);
}
}
int main()
{
int i,j;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",&p[i]);
for(i=;i<=n;i++)scanf("%d",&d[i]);
for(i=;i<=n;i++)scanf("%d",&j),e[j].push_back(i);
long long ret=;
for(j=;j<=n;j++)
{
if(vis[j])continue;
tot=;
dfs(j);
if(!tot)continue;
for(i=;i<=tot;i++)dfs1(cir[i]);
if(tot==){ret+=dp[cir[]][];continue;}
g[][]=dp[cir[]][],g[][]=dp[cir[]][];
for(i=;i<=tot;i++)
{
g[i][]=min(g[i-][]+dp[cir[i]][],g[i-][]+dp[cir[i]][]-p[cir[i]]);
g[i][]=dp[cir[i]][]+g[i-][];
}
long long cur=g[tot][];
g[][]=dp[cir[]][]-p[cir[]];
g[][]=1e18;
for(i=;i<=tot;i++)
{
g[i][]=min(g[i-][]+dp[cir[i]][],g[i-][]+dp[cir[i]][]-p[cir[i]]);
g[i][]=dp[cir[i]][]+g[i-][];
}
ret+=min(cur,g[tot][]);
}
printf("%lld\n",ret);
return ;
}
牛客网暑期ACM多校训练营(第二场)B discount的更多相关文章
- 牛客网暑期ACM多校训练营 第九场
HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
- 牛客网暑期ACM多校训练营(第五场):F - take
链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学
牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...
- 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)
牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第七场)Bit Compression
链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
随机推荐
- C/C++带有空格的字符串输入
一.带有空格的字符串输入 (一)C++篇 1. 针对字符数组而言 方法一 getline() 读入整行数据,使用回车键输入换行符来确定输入结尾. 调用方法: cin.getline(str,len) ...
- 洛谷 P1600 天天爱跑步
https://www.luogu.org/problemnew/show/P1600 (仅做记录) 自己的假方法: 每一次跑从a到b:设l=lca(a,b)对于以下产生贡献: a到l的链上所有的点( ...
- Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常
String[] queryNames = request.getParameterValues("queryName"); List<String> queryNam ...
- poj3233Matrix Power Series
链接 也是矩阵经典题目 二分递归求解 a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)( ...
- Dev之GridControl详解
Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多半借助Demo和英文帮助文档.网上具体的使用方法也多半零碎.偶遇一个简单而且 ...
- CCF|游戏|Java
import java.util.Scanner; public class tyt { public static void main(String[] args) { Scanner in = n ...
- oracle 时间格式转化以及计算
--A表中的日期字段 create_date 例如:2017-08-05 转化为2017年8月5日 oracle 在这里的双引号会忽略 select to_char(to_date(tt.c ...
- 掌握Spark机器学习库-06-基础统计部分
说明 本章主要讲解基础统计部分,包括基本统计.假设检验.相关系数等 数据集 数据集有两个文件,分别是: beijing.txt 北京历年降水量,不带年份 beijing2.txt 北京历年降水量,带年 ...
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- mysql中判断条件
if / case when 判断 SELECT CASE 1 WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "m ...