zjnu1762 U (线段树)
Description
Mirko is hungry as a bear, scratch that, programmer and has stumbled upon a local restaurant. The restaurant offers N meals and has an interesting pricing policy: each meal i
has two assigned prices, Ai and Bi . Mirko pays A only for the first ordered meal, while B prices apply for all other meals.
Mikro can't decide how many meals to order. In order to make his decision easier, he has asked you to compute, for each k between 1 i N (inclusive), the minimum total price
for k ordered meals. Mirko doesn't care which particular meals he orders or in which order he orders them, however he won't order the same meal twice. Order, order,
order.
Input
The first line of input contains the positive integer N (2 ≤ N ≤ 500 000), the number of different meals offered by the restaurant. Each of the following N lines contains two
positive integers, Ai and Bi (1 ≤ Ai , Bi ≤ 1 000 000 000), the prices for meal i as described above.
Output
Output must consist of N lines, where line k contains the minimum price for ordering exactly k different meals.
Sample Input
10 5
9 3
10 5
2
100 1
1 100
5
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
Sample Output
13
18
1
2
1000000000
2000000000
3000000000
4000000000
5000000000
Hint
Clarification of the first example:
k = 1: Mirko pays A2 = 9 for the starting meal 2.
k = 2: Mirko pays A1 = 10 for the starting meal 1, then B2 = 3 for meal 2.
k = 3: Mirko pays A1 = 10 for the starting meal 1, then B2 = 3 for meal 2, and finally B3= 5 for meal 3.
题意:有n道菜,每次选择吃菜的时候,第一道菜的价值是a[i],之后每一道选的菜的价值为b[i],问选择k道菜,最少花的价钱是多少。
思路:题解的思路很巧妙,因为要使得选k道菜的价钱最少,只有两种可能,一种是选择前k道b[i]价值最少的,然后把这k道菜中其中的一道菜的价值变为a[i];另一种是先选择前k-1道菜,然后在不属于这k-1道菜中的其他的菜选择一道a[i]价值最少的,作为第1道菜。前一种可以用set做,后一种用线段树做,然后取小就行了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 500050
ll sum[maxn];
set<ll>myset;
set<ll>::iterator it;
ll ans[maxn];
struct edge{
ll x,y;
}a[maxn];
bool cmp(edge a,edge b){
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
struct node{
int l,r,minx;
}b[4*maxn];
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
if(l==r){
b[th].minx=a[l].x;
return;
}
mid=(l+r)/2;
build(l,mid,th*2);
build(mid+1,r,th*2+1);
b[th].minx=min(b[th*2].minx,b[th*2+1].minx);
}
int question(int l,int r,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
return b[th].minx;
}
mid=(b[th].l+b[th].r)/2;
if(r<=mid)return question(l,r,th*2);
else if(l>mid)return question(l,r,th*2+1);
else{
return min(question(l,mid,th*2),question(mid+1,r,th*2+1) );
}
}
int main()
{
int n,m,i,j,k;
while(scanf("%d",&n)!=EOF)
{
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%lld%lld",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp);
build(1,n,1);
myset.clear();
for(k=1;k<=n;k++){
ans[k]=sum[k-1]+question(k,n,1);
myset.insert(a[k].x-a[k].y);
sum[k]=sum[k-1]+a[k].y;
ans[k]=min(ans[k],sum[k]+(*myset.begin()) );
printf("%lld\n",ans[k]);
}
}
return 0;
}
zjnu1762 U (线段树)的更多相关文章
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- codevs 1080 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树
#44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
随机推荐
- 操作系统-1w字关于内存的总结
内存的基本概念 什么是内存,有何作用 内存是用于存放数据的硬件.程序执行前需要先放入内存中才能被CPU处理 存储单元 内存中也有一个一个的小房间,每个小房间就是一个存储单元. 如果计算机按照 字节编址 ...
- 基于腾讯云存储网关 CSG 实现视频在线转码分发
一.背景 随着越来越多的传统业务云化和云端业务发展,数据上云和云端数据处理领域的需求爆发式增长.腾讯云存储网关CSG提供一键部署开箱即用的便捷模式,深度结合COS对象存储生态,为用户提供方便快捷的数据 ...
- python函数3-函数嵌套/递归/匿名函数
2 .函数递归: 3.匿名函数
- 【MySQL】MySQL知识图谱
MySQL 文章目录 MySQL 表 锁 索引 连接管理 事务 日志系统 简单记录 极客时间 - MySQL实战45讲 MySQL知识图谱 表 表 引擎选择 编码问题 表空间管理 字段设计 备份和恢复 ...
- ECharts图表——封装通用配置
前言 前段时间在做大屏项目,大量用到echarts图表,大屏对设计规范要求比较高,而大屏项目,经常会因为业务方面的原因.或者是数据方面的原因改动UI设计,所有图表的代码也是三天一小改.五天一大改 因此 ...
- 1.2V升压到3V和3.3V的升压芯片
1.2V镍氢电池升压到3V和3.3V输出,1.2V升压3V,1.2V升压3.3V稳压输出供电的芯片. PW5100 是一款低静态电流.达效率. PFM 模式控制的同步升压变换器. PW5100 所需的 ...
- 带你走进memcache,老牌内存缓存技术
一.核心优化概述 什么是优化:以更小的资源支持更大负载网站的运行,以小博大. 思路:尽量减少用户等待时间,节省系统资源开销,节省带宽使用. 优化什么地方?有三方面:Memcache内存缓存技术.静态化 ...
- Mybatis Plus 3.4版本之后分页插件的变化
一.MybatisPlusInterceptor 从Mybatis Plus 3.4.0版本开始,不再使用旧版本的PaginationInterceptor ,而是使用MybatisPlusInter ...
- 笔记 | pandas之时间序列学习随笔1
1. 时间序列自动生成 ts = pd.Series(np.arange(1, 901), index=pd.date_range('2010-1-1', periods=900)) 最终生成了从20 ...
- MySQL之谓词下推
MySQL之谓词下推 什么是谓词 在SQL中,谓词就是返回boolean值即true或者false的函数,或是隐式转换为boolean的函数.SQL中的谓词主要有 LKIE.BETWEEN.IS NU ...