zoukankan      html  css  js  c++  java
  • 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    【题目链接】:http://codeforces.com/contest/822/problem/C

    【题意】

    有n个旅行计划,
    每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述;
    让你在这n个旅行计划中选出两个计划;
    要求这两个计划的日期没有相交的部分;
    且这两个日期的总时间长度恰好为x;
    让你求最小花费

    【题解】

    先把每个计划按照左端点第一优先级,右端点第二优先级升序排序;
    然后定义一个dp[x]数组,表示在前i个计划中,时长为x,且右端点的位置< a[i].l的一个旅行计划的最小花费;
    更新的时候:
    len = a[i].r-a[i].l+1;
    ans = min(ans,a[i].cost+dp[x-len]);
    对于到达的第i个计划;
    先看看有没有哪个之前哪个计划从这个位置开始才满足r< a[i].l的;用那些来更新dp数组;
    然后在更新完毕之后,在找到这个第i个计划从哪个位置开始,满足a[i].r< l暂时先存着.

    【Number Of WA

    1

    【反思】

    在写的时候,把二分答案的初始值给赋错了;
    很伤心。
    还是缺少经验啊。

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int INF = 2e5+100;
    const int N = 2e5+100;
    
    struct abc{
        int l,r,cost;
    };
    
    int n,x,dic[INF],ans = 21e8;
    abc a[N];
    vector <abc> need[N];
    
    bool cmp(abc a,abc b){
        if (a.l != b.l)
            return a.l < b.l;
        else
            return a.r < b.r;
    }
    
    int main(){
        //Open();
        Close();
        cin >> n >> x;
        rep1(i,1,n){
            cin >> a[i].l >> a[i].r >> a[i].cost;
        }
        sort(a+1,a+1+n,cmp);
        abc temp;int len;
        rep1(i,1,n){
            while ( (int) need[i].size()){
                temp = need[i].back();
                need[i].pop_back();
                len = temp.r-temp.l+1;
                if (!dic[len]){
                    dic[len] = temp.cost;
                }else{
                    dic[len] = min(dic[len],temp.cost);
                }
            }
            temp = a[i];
            len = temp.r - temp.l+1;
            if (x >= len && dic[x-len]){
                ans = min(ans,temp.cost+dic[x-len]);
            }
            int l = i +1 ,r = n;
            int idx = n+1;
            while (l <= r){
                int mid = (l+r)>>1;
                if (a[mid].l > a[i].r){
                    idx = mid;
                    r = mid - 1;
                }else
                    l = mid + 1;
            }
            need[idx].push_back(temp);
        }
        if (ans > (int) 20e8)
            cout << -1 << endl;
        else
            cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    vue组件---动态组件之多标签页面
    vue组件---动态组件&异步组件
    vue组件---插槽
    vue组件---自定义事件
    ES6字符串模板
    vue组件---组件注册
    vue基础---表单输入绑定
    vue基础---事件处理
    从浏览器输入 URL 到页面展示过程
    表单提交type=submit和type=image的区别
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626222.html
Copyright © 2011-2022 走看看