zjnu1762 U (线段树)

来源:互联网 发布:debian官方软件源 编辑:程序博客网 时间:2024/04/28 13:53

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 52 100 1 1 1005 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output

9 13 181210000000002000000000300000000040000000005000000000

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 500050ll 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;}


0 0
原创粉丝点击