#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;
 5 const int N=2e5+10;
 6
 7  vector<pair <LL ,LL > > l[N],r[N];
 8  vector<pair <LL ,LL > >::iterator it;
 9 LL c[N];
10
11 int main(){
12     int n,x;
13     int ll,rr,v;
14     scanf("%d%d",&n,&x);
15     for(int i=1;i<=n;i++){
16         scanf("%d%d%d",&ll,&rr,&v);
17         l[ll].push_back(make_pair(rr-ll+1,v));
18         r[rr].push_back(make_pair(rr-ll+1,v));
19     }
20     for(int i=1;i<=200000;i++) c[i]=INF;
21     LL ans=INF;
22     for(int i=1;i<=200000;i++){
23         for(int j=0;j<l[i].size();j++){
24             int y=l[i][j].first;
25             if(y>=x) continue;
26             if(c[x-y]!=INF) ans=min(ans,c[x-y]+l[i][j].second);
27         }
28         for(int j=0;j<r[i].size();j++){
29             int y=r[i][j].first;
30             c[y]=min(c[y],r[i][j].second);
31         }
32     }
33     if(ans==INF) cout<<-1<<endl;
34     else
35     cout<<ans<<endl;
36 }
时间: 2024-08-29 11:36:57

#422(div2)C. Hacker, pack your bags!的相关文章

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) 表

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 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,v

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",&

#422(div2)B. Crossword solving

题意:给出2个字符串,A,B,A长度严格小于B长度,问改动A多少个字符,能成为B的子串,求最少改动 思路:暴力,2层FOR循环,可用set来存储已B的第i个字符为首需要改动的位置 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 set<int >a[1002]; 5 int main(){ 6 int n,m; 7 cin>>n>>m; 8 string s

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 (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