码迷,mamicode.com
首页 > 其他好文 > 详细

codeforce——思维dp

时间:2018-02-26 21:40:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   namespace   cin   class   type   typedef   选择   int   name   

题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少。

#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
const int INF = 0x3f3f3f3f;
const int moder = 10000;
const int maxn = 2000000;
const int M = 2e5+10;
struct node
{
    int l;int r;int cs;
};

bool cmp1(const node &a,const node &b)
{
    return a.l < b.l;
}
bool cmp2(const node &a, const node &b)
{
    return a.r < b.r;
}
ll mincost[M];

ll gmin(ll a,ll b)
{
    return a>b?b:a;
}

int main()
{
    int n,x;
    cin >> n >> x;
    node save1[n];
    node save2[n];
    for(int i=0;i<M;i++)
        mincost[i]=INT_MAX;
    for(int i=0;i < n;i++)
    {
        cin >> save1[i].l >> save1[i].r >> save1[i].cs;
        save2[i] = save1[i];
    }
    sort(save1,save1+n,cmp1);
    sort(save2,save2+n,cmp2);

    int minn = INT_MAX;
    int j=0;
    for(int i=0;i < n;i++)
    {
        while(j < n&&save2[j].r < save1[i].l)
        {
            mincost[save2[j].r-save2[j].l+1] = gmin(mincost[save2[j].r-save2[j].l+1],save2[j].cs);
            j++;
        }
        int k = x-(save1[i].r-save1[i].l+1);
        if(k > 0&&minn > mincost[k]+save1[i].cs)
            minn = mincost[k]+save1[i].cs;
    }
    if(minn ==  INT_MAX)
        printf("-1\n");
    else
        printf("%d\n",minn);

    return 0;
}

——

codeforce——思维dp

标签:style   namespace   cin   class   type   typedef   选择   int   name   

原文地址:https://www.cnblogs.com/cunyusup/p/8475936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!