CodeForces 822C Hacker, pack your bags!

题意

给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低

思路

相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中,对时间 i 从前向后扫,在对应的k-i中二分找第一个不重合的区间,其对应的minn加上 i 的cost即为出发时间为 i 时的最优解

代码

#include<bits/stdc++.h>
using namespace std;
int n, k;
struct EVE{
    int st,ed,val;
    EVE(){
    }
    EVE(int a,int b, int c){
        st = a, ed = b, val = c;
    }
};
int f,t,c;
vector<EVE> v[200200];
vector<int> minn[200200];
int tmp[200200];
bool cmp(EVE a, EVE b){
    return a.st<b.st;
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i = 0;i<n;i++){
        scanf("%d%d%d",&f,&t,&c);
        if(t-f+1 >= k) continue;
        v[t-f+1].push_back({f,t,c});
    }
    for(int i = 1;i<=k;i++) sort(v[i].begin(),v[i].end(),cmp);
    for(int i = 1;i<=k;i++){
        for(int j = v[i].size()-1;j>=0;j--){
            if(j==v[i].size()-1) tmp[j]=v[i][j].val;
            else tmp[j]=min(v[i][j].val,tmp[j+1]);
        }
        for(int j = 0;j<v[i].size();j++){
            minn[i].push_back(tmp[j]);
        }
    }
    long long ans = 1e12;
    for(int i = 1;i<=k;i++){
        if(v[k-i].empty()) continue;
        for(int j = 0;j<v[i].size();j++){
            int ed = v[i][j].ed;
            long long cost = v[i][j].val;
            int le = 0, ri = v[k-i].size()-1;
            if(v[k-i][ri].st<=ed) continue;
            int mid = le+ri>>1;
            while(le<ri){
                mid = le+ri>>1;
                if(v[k-i][mid].st<=ed) le = mid+1;
                else ri = mid;
            }
            ans = min(ans, cost+minn[k-i][le]);
        }
    }
    if(ans == 1e12) printf("-1");
    else printf("%I64d",ans);
    return 0;
} 
时间: 2024-11-15 00:23:34

CodeForces 822C Hacker, pack your bags!的相关文章

Codeforces 822C Hacker, pack your bags! - 贪心

It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha. So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world.

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

C. Hacker, pack your bags! It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha. So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack co

Codeforces Round #422 (Div. 2) C Hacker, pack your bags!

It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha. So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world.

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心

链接: http://codeforces.com/contest/822/problem/C 题意: 有x天的假期, 有n张旅行票, 每张票有起始时间l, 结束时间r, 花费cost, 想把假期分成两部分出去旅游, 两部分时间不能重合(ri < lj || rj < li), 问最小花费是多少, 如果不能两部分, 输出-1 题解: CF官方解法, 效率O(nlogn2) 设置一个结构体, struct P{int p, len, cost, type}; 将每张票(l, r, cost) 表

#422(div2)C. Hacker, pack your bags!

题意:给出n个区间和X,每个区间有左右边界和价值,li,ri,x.然后问从这n个区间找出2个不重合的区间,他们的区间长度和为x,并且价值最小 思路:我们可以预处理出每个长度所包含的区间,然后可以保存每个区间L的位置,和R的位置,那么在某个位置(某个L),前面的都是无重合,在判断下X-当前区间的长度 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const long long INF=1e18

Codeforces Round #422 C

Hacker, pack your bags! 题意:给n个区间,每个区间的长度为ri-li+1,权值为ci,选取2个不相交的区间,长度加起来为x,且权值和最小 思路:遍历 l ,vi[l]存起点为l的区间的权值最小值,每次加入li==l的区间更新vi的值,更新后再更新ri==l-1的答案,这样可以保证,每次选择一个区间后,从vi里选择的另一个区间一定是不相交的 AC代码: #include<bits/stdc++.h> #include "iostream" #inclu

codeforces round 422 div2 补题 CF 822 A-F

A I'm bored with life 水题 #include<bits/stdc++.h> using namespace std; typedef long long int LL; const LL N=1,M=1,MOD=1; int main() {//freopen("t.txt","r",stdin); ios::sync_with_stdio(false); LL a,b; scanf("%I64d%I64d",&

Codeforces Round #422 (Div. 2) A-C

A. I'm bored with life 水题 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <iomanip> #include <math.h> #include <map> using name

ACM团队周赛题解(3)

940和822两套div.2 老规矩 #define MAXN 1000000+5#define MOD 1000000007#define PI (acos(-1.0))#define EPS 1e-6#define MMT(s,a) memset(s, a, sizeof s)#define GO(i,a,b) for(int i = (a); i < (b); ++i)#define GOE(i,a,b) for(int i = (a); i <= (b); ++i)#define OG