UESTC - 482 Charitable Exchange(线段树)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1059752567/article/details/70142302

题意:一开始明星有一块钱,他把一块钱拿去换其他价值的东西使得最后价值大于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 11 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 RiRi yuan, with a time cost of TiTi minutes.

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

Input

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

For each case, two integers NNMM (1N1051≤N≤1051M1091≤M≤109) in the first line indicates the number of available exchanges and the expected value of final item. Then NN lines follow, each line describes an exchange with 33 integers ViViRiRiTiTi (1RiVi1091≤Ri≤Vi≤1091Ti1091≤Ti≤109).

Output

For every test case, you should output Case #k: first, where kk indicates the case number and counts from 11. 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


猜你喜欢

转载自blog.csdn.net/qq1059752567/article/details/70142302