Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

题目连接:

http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E

Description

Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can move only from left to right, he can pass one unit of length each second.

Now it‘s raining, so some segments of his way are in the rain. Formally, it‘s raining on n non-intersecting segments, the

i-th segment which is in the rain is represented as [li,ri] (0≤li<ri≤a).

There are m umbrellas lying on the line, the i-th umbrella is located at point xi (0≤xi≤a) and has weight pi. When Polycarp begins his journey, he doesn‘t have any umbrellas.

During his journey from x=0 to x=a Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn‘t want to get wet, he must carry at least one umbrella while he moves from x to x+1 if a segment [x,

x+1] is in the rain (i.e. if there exists some i such that li≤x and x+1≤ri).

The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.

Each unit of length passed increases Polycarp‘s fatigue by the sum of the weights of umbrellas he carries while moving.

Can Polycarp make his way from point x=0 to point x=a? If yes, find the minimum total fatigue after reaching x=a, if Polycarp picks up and throws away umbrellas optimally.

Sample Input

10 2 4
3 7
8 10
0 10
3 4
8 1
1 2

Sample Output

14

题意

有几段下雨的地方,有几把雨伞在地上,消耗的值为伞的重量*移动距离,问在不被淋湿的情况下,如何打伞消耗最小

题解:

dp[i]指的是从第i把伞开始打之后的最小消耗,他由dp[j] (j>i)转移而来。

时间复杂度O(m^2)

代码

#include <bits/stdc++.h>

using namespace std;

pair<int, int> r[2010];
pair<int, int> u[2010];
int n, m, a;
int h[2010];
int ans;
const int INF = 0x7fffffff;
int st, fn;

int main() {
    //freopen("1.txt","r",stdin);

    cin >> a;
    cin >> n >> m;
    st = INF;
    fn = 0;
    for (int i = 0; i < n; i++) {
        cin >> r[i].first >> r[i].second;
        st = min(st, r[i].first);
        fn = max(fn, r[i].second);
    }
    for (int i = 0; i < m; i++) cin >> u[i].first >> u[i].second;
    sort(u, u + m, [](const pair<int, int> &p, const pair<int, int> &q) { return p < q; });
    for (int i = 0; i <= m; i++) {
        h[i] = INF;
    }
    sort(r, r + n, [](const pair<int, int> &p, const pair<int, int> &q) { return p < q; });
    for (int i = m - 1; i >= 0; i--) {
        int index;
        for (index = n - 1; index >= 0; index--)
            if (r[index].first < u[i].first) break;
        int cur = (fn > u[i].first ? fn - u[i].first : 0) * u[i].second;
        h[i] = min(h[i], cur);

        for (int j = 0; j < i; j++) {
            int cur;
            if (r[index].second > u[j].first) {
                cur = (min(r[index].second, u[i].first) - u[j].first) * u[j].second;
            } else {
                cur = 0;
            }
            h[j] = min(h[j], h[i] + cur);
        }
    }

    ans = INF;
    for (int i = 0; i < m; i++) {
        if (u[i].first <= st) ans = min(ans, h[i]);
    }
    if (ans == INF) ans = -1;
    cout << ans << endl;
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9153687.html

时间: 2024-10-09 16:23:41

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas的相关文章

Codeforces Round #486 (Div. 3) C. Equal Sums

Codeforces Round #486 (Div. 3) C. Equal Sums 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/C Description You are given k sequences of integers. The length of the i-th sequence equals to ni. You have to choose exactly two sequences

Codeforces Round #486 (Div. 3) A. Diverse Team

Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Description There are n students in a school class, the rating of the i-th student on Codehorses is ai. You have to form a team consisting of k students

Codeforces Round #486 (Div. 3) B. Substrings Sort

Codeforces Round #486 (Div. 3) B. Substrings Sort 题目连接: http://codeforces.com/contest/988/problem/B Description You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for e

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/D Description There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. Choose a subset of

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #392 (Div. 2) F. Geometrical Progression

原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output For given n, l and r find the number of distinct geometrical pro

Codeforces Round #486 (Div. 3)

988A.http://codeforces.com/contest/988/problem/A 题意:给出n个数,让你从中取出m个不同的数组成一组 分析:模拟即可.当每个人为第一次出现时,标记这个位置可取.最后从可取的位置取m个即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 const in

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art

Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\),\(pref_j\),一个人可以买一道菜的条件是 1. \(p_i \leq inc_j \leq s_i\) 2. \(|b_i - pref_j| \leq inc_j-p_i\) ,问每个人分别能买多少道菜 题解 转化一下公式 \(p_i \leq inc_j \leq s_i\) 下面两个满