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 ...
随机推荐
- Flutter 基础组件:状态管理
前言 一个永恒的主题,"状态(State)管理",无论是在React/Vue(两者都是支持响应式编程的Web开发框架)还是Flutter中,他们讨论的问题和解决的思想都是一致的. ...
- 🎉 Element UI for Vue 3.0 来了!
第一个使用 TypeScript + Vue 3.0 Composition API 重构的组件库 Element Plus 发布了 ~ 2016 年 3 月 13 日 Element 悄然诞生,经历 ...
- Upload - Labs (下)
Pass - 11: 1.查看源代码,发现进行了一次对后缀名替换成空格,因此考虑双写绕过, 2.上传成功, 关键代码: $is_upload = false; $msg = null; if (iss ...
- Inlook - 你的私人工作助理 V1.0.0.2
Inlook - Your personal assistant 中文版|English version Introduction Inlook是为在桌面上直观地提醒用户收到未读邮件和日程安排而开发的 ...
- SAP RFC的相关的术语说明
工作比较忙,很少有时间写点文章,抽空写点吧,给需要的人看看,虽然徒弟很多了,不过还是不要固步自封,在这里也指导更多的人进步吧. RFC(Remote Function Call)是SAP系统和其他(S ...
- 日常分享:关于时间复杂度和空间复杂度的一些优化心得分享(C#)
前言 今天分享一下日常工作中遇到的性能问题和解决方案,比较零碎,后续会持续更新(运行环境为.net core 3.1) 本次分享的案例都是由实际生产而来,经过简化后作为举例 Part 1(作为简单数据 ...
- JWT令牌简介及demo
一.访问令牌的类型 二.JWT令牌 1.什么是JWT令牌 JWT是JSON Web Token的缩写,即JSON Web令牌,是一种自包含令牌. JWT的使用场景: 一种情况是webapi,类似之 ...
- PCB导线长宽与电源压降
为了计算PCB中电源线走线后的压降,需要知道PCB中使用的铜的电阻率, PCB板中的铜是直接贴上去的铜箔,因此可以当成纯铜(我问了PCB打样的厂家他们的铜的电阻率,但是他们给我说不知道,所以干脆就当成 ...
- 大数据谢列3:Hdfs的HA实现
在之前的文章:大数据系列:一文初识Hdfs , 大数据系列2:Hdfs的读写操作 中Hdfs的组成.读写有简单的介绍. 在里面介绍Secondary NameNode和Hdfs读写的流程. 并且在文章 ...
- 快速排序与荷兰国旗及Partition问题
快速排序与荷兰国旗及Partition问题 需求: 1.Partition过程 给定一个数组arr,和一个整数num.请把小于等于num的数放在数组的左边,大于num的数放在数组的右边. 要求额外空间 ...