UESTC - 482 Charitable Exchange(线段树)

题意:一开始明星有一块钱,他把一块钱拿去换其他价值的东西使得最后价值大于M并时间最短,无解输出-1.

思路:按照需求R升序排序,并且按照可达价值作为下标,所用时间作为值进行线段树建树。

#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <iostream>
#include <map>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const double PI = acos(-1.0);
const double EPS = 1e-9;
using namespace std;

const int maxm = 3e5+5;
const int maxn = 1e5+5;
const long long INF = 1234567890123456;
#define LL long long

LL tree[maxm<<2];
LL X[maxm];

void build(int l,int r,int rt)
{
    tree[rt] = INF;
    if(l==r)
        return;
    int m = (l+r)>>1;
    build(lson);
    build(rson);
}

struct Node
{
    LL v,R,t;
    bool operator<(const Node& cmp) const
    {
        return R<cmp.R;
    }
}node[maxn];

void update(int o,LL c,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt] = min(tree[rt],c);
        return;
    }
    int m = (l+r)>>1;
    if(o<=m)
        update(o,c,lson);
    else
        update(o,c,rson);
    tree[rt] = min(tree[rt<<1],tree[rt<<1|1]);
}

LL query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return tree[rt];
    int m = (l+r)>>1;
    LL ret = INF;
    if(L<=m)
        ret = min(ret,query(L,R,lson));
    if(R>m)
        ret = min(ret,query(L,R,rson));
    return ret;
}

int main()
{
    int T,cas = 1;
    scanf("%d",&T);
    while(T--)
    {
        int n,tol=0;
        LL M;
        scanf("%d",&n);
        scanf("%lld",&M);
        X[++tol] = 1;
        X[++tol] = M;
        for(int i=0; i<n; i++)
        {
            scanf("%lld%lld%lld",&node[i].v,&node[i].R,&node[i].t);
            X[++tol] = node[i].v;
            X[++tol] = node[i].R;
        }
        sort(node,node+n);
        sort(X+1,X+1+tol);
        tol = unique(X+1,X+1+tol)-X;
        tol--;

        build(1,tol,1);
        update(1,0,1,tol,1);
        for(int i=0; i<n; i++)
        {
            int R = lower_bound(X+1,X+1+tol,node[i].R)-X;
            LL tmp = query(R,tol,1,tol,1);
            int v = lower_bound(X+1,X+1+tol,node[i].v)-X;
            update(v,node[i].t+tmp,1,tol,1);
        }
        int t = lower_bound(X+1,X+1+tol,M)-X;
        LL tmp = query(t,tol,1,tol,1);
        printf("Case #%d: ",cas++);
        if(tmp==INF)
            puts("-1");
        else
            printf("%lld\n",tmp);
    }
	return 0;
}


Charitable Exchange

Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values  1 1 yuan. Then, through the efforts of repeatedly exchanges which continuously increase the value of item in hand, he (she) finally brings back a valuable item and donates it to the needy.

In each exchange, one can exchange for an item of Vi yuan if he (she) has an item values more than or equal to  Ri Ri yuan, with a time cost of  Ti Ti minutes.

Now, you task is help the star to exchange for an item which values more than or equal to  M M yuan with the minimum time.

Input

The first line of the input is  T T (no more than  20 20), which stands for the number of test cases you need to solve.

For each case, two integers  N N M M ( 1N105 1≤N≤105 1M109 1≤M≤109) in the first line indicates the number of available exchanges and the expected value of final item. Then  N N lines follow, each line describes an exchange with  3 3 integers  Vi Vi Ri Ri Ti Ti ( 1RiVi109 1≤Ri≤Vi≤109 1Ti109 1≤Ti≤109).

Output

For every test case, you should output Case #k: first, where  k k indicates the case number and counts from  1 1. Then output the minimum time. Output  1 −1 if no solution can be found.

Sample input and output

Sample Input Sample Output
3
3 10
5 1 3
8 2 5
10 9 2
4 5
2 1 1
3 2 1
4 3 1
8 4 1
5 9
5 1 1
10 4 10
8 1 10
11 6 1
7 3 8
Case #1: -1
Case #2: 4
Case #3: 10


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值