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

 
 
3 10 5 9 3 10 5 2 100 1 1 100 5 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output

 
 
9 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;
}




转载于:https://www.cnblogs.com/herumw/p/9464512.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值