牛客网暑期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 ...
随机推荐
- [poj3744] Scout YYF I【概率dp 数学期望】
传送门:http://poj.org/problem?id=3744 令f(i)表示到i,安全的概率.则f(i) = f(i - 1) * p + f(i - 2) * (1 - p),若i位置有地雷 ...
- redis持久化和分布式实现
Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势. 本文使用的redis是3.2.1版本.下载后,文件如下 将文件解压到指 ...
- 安装CentOS--设置网络_2
(1)虽然CentOS 7已经可以联网,但是在日常的运维工作中,我们是需要手动给Linux系统设置IP地址的.输入如下命令. # vi /etc/sysconfig/network-scripts/i ...
- 安装CentOS--设置网络_1
(1)在登录黑框中输入如下命令,让CentOS 7自动获取一个IP地址: # dhclient (2)正常情况下不会有任何输出内容.用如下命令查看获取到的IP地址: # ip addr 它将返回如图所 ...
- chosen-bootstrap使用技巧
1.页面加载完成后,通过js方式设置值,无法有效显示的问题. 解决:先设置值,让后在进行初始化操作. // 设置select选中值 $("#type").val(type); // ...
- JDK1.8中的Stream详解
Stream简介 Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 XML ...
- php简单实用的调试工具类
<?php /* * 调试类 */ class Common_Debug { //打开错误报告 public static function showError($debug = true) { ...
- Server.MapPath() 用法
Server.MapPath() ./当前目录/网站主目录../上层目录~/网站虚拟目录 如果当前的网站目录为E:\wwwroot 应用程序虚拟目录为E:\wwwroot\company 浏览的页 ...
- torch.nn.Linear()函数的理解
import torch x = torch.randn(128, 20) # 输入的维度是(128,20)m = torch.nn.Linear(20, 30) # 20,30是指维度output ...
- Libjingle 库
Libjingle 是google talk voice(语音聊天) 和 p2p interoperability(点对点操作)库,是提供了google talk,p2p文件共享和语音呼叫能力的组件集 ...