codeforces 某套题s : surf(贪心 || 动态规划)

题目:

 Now that you’ve come to Florida and taken up surfing, you love it! Of course, you’ve realized that if you take a particular wave, even if it’s very fun, you may miss another wave that’s just about to come that’s even more fun. Luckily, you’ve gotten excellent data for each wave that is going to come: you’ll know exactly when it will come, how many fun points you’ll earn if you take it, and how much time you’ll have to wait before taking another wave. (The wait is due to the fact that the wave itself takes some time to ride and then you have to paddle back out to where the waves are crashing.) Obviously, given a list of waves, your goal will be to maximize the amount of fun you could have.

题意:

大体上就是给你n组数据,区间开始,权值的大小,区间持续时间(即区间的结束时间),选择其中的区间,使其最终权值最大。

详细题意自己翻吧。

思路:

贪心的思想,也有点像背包,首先将n个区间按结束时间排序,然后背包每个区间选与不选使其最终权值最大。

具体看代码。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <cstdlib>
using namespace std;
#define is_lower(c) (c>=‘a‘ && c<=‘z‘)
#define is_upper(c) (c>=‘A‘ && c<=‘Z‘)
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c>=‘0‘ && c<=‘9‘)
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define IO ios::sync_with_stdio(0);\
    cin.tie(0);    cout.tie(0);
#define For(i,a,b) for(int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
const ll inf=0x3f3f3f3f;
const double EPS=1e-10;
const ll inf_ll=(ll)1e18;
const ll maxn=100005LL;
const ll mod=1000000007LL;
const int N = 1e6+5;
struct node {
    int s,e;
    int score;
}r[300005];
bool cmp(node a,node b) {
    return a.e <b.e;
}
long long dp[N*2];
int main() {
    int T;
    scanf("%d",&T);
    for(int i = 1; i <= T; i++){
        int x ;
        scanf("%d%d%d",&r[i].s,&r[i].score,&x);
        r[i].e = r[i].s + x;
    }
    sort(r+1,r+T+1,cmp);
    for(int i = 1; i <= T;i++){
        dp[r[i].e] = max(dp[r[i].s]+r[i].score,dp[r[i].e]);
        for(int j = r[i].e+1; j <= r[i+1].e;j++)  //此循环代表若不选它的下一区间权值的最大值。
            dp[j] = dp[r[i].e];
    }
    printf("%lld\n",dp[r[T].e]);
}

/*
4
8 50 2
10 40 2
2 80 9
13 20 5


10
2079 809484 180
8347 336421 2509
3732 560423 483
2619 958859 712
7659 699612 3960
7856 831372 3673
5333 170775 1393
2133 989250 2036
2731 875483 10
7850 669453 842
*/

 

原文地址:https://www.cnblogs.com/GHzz/p/8724023.html

时间: 2024-07-29 16:35:14

codeforces 某套题s : surf(贪心 || 动态规划)的相关文章

Educational Codeforces Round 15 套题

这套题最后一题不会,然后先放一下,最后一题应该是大数据结构题 A:求连续最长严格递增的的串,O(n)简单dp #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <vector> usi

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Moscow Pre-Finals Workshop 2016. Japanese School OI Team Selection. 套题详细解题报告

写在前面 谨以此篇题解致敬出题人! 真的期盼国内也能多出现一些这样质量的比赛啊.9道题中,没有一道凑数的题目,更没有码农题,任何一题拿出来都是为数不多的好题.可以说是这一年打过的题目质量最棒的五场比赛之一了!(其中G.I和D题简直是好题中的好题!) 由于网上没有任何这套题的题解,我每道题都绞尽脑汁想了好久(尤其是D题.I题和H题证明),在此认认真真的写一篇博客,也真心希望好题能被更多的人发现和赞美. 题目概述 题目 思维难度  推荐指数 考点 A 3  ☆☆☆☆ 最长上升子序列 B 暂留坑,>7

[Codeforces 1295E]Permutation Separation(线段树+贪心)

[Codeforces 1295E]Permutation Separation(线段树+贪心) 题面 给出一个排列\(p_1,p_2,...p_n\).初始时你需要选择一个位置把排列分成左右两个.然后在两个序列间移动元素使得左边序列的所有元素都比右边的所有元素小.给出每个元素\(p_i\)从一个序列移动到另一个序列的代价\(a_i\). 分析 显然最后得到的序列是小的数在一边,大的数在另一边.设从值为\(i\)的元素处分开之后移动代价为\(ans_i\). 一开始假设所有数都移到右边序列,那么

2017年8月14日套题记录 | 普及组

写在前面 今天登洛谷发现离Noip剩下88天了??(虽然看起有点久),然后觉得似乎水了一个暑假什么也没做(虽然学了点数据结构和一些奇奇Gaygay的东西),于是打开题库发现去年Long Happy的集训套题我似乎没有提交过,那就一天一套题,顺便码个题解+心得(雾? T2.传作业 题目描述 某十三同学一日上学迟到,此时已经开始上早自习了,所以他只好请同学帮忙把作业传到组长那里.由于刚开学不久,某十三同学还没来得及认识所有同学,所以传作业时只好找熟悉的同学.已知某十三与组长之间有N个他熟悉的同学,并

应试教育:你是在“套题”还是在“解决问题”?

?为什么当初会采用"背题型"的方式? ?"背题型"的局限性在哪? ?建构"知识体系"优势在哪? ?"题海战术"的认知错误 ?学习和考试的本质 声明:此文纯属个人学习方法总结. 1.为什么当初会采用"背题型"的方式? 以前上了高中,由于身边学霸太多,为了急于缩短与他们之间的天壤之别,总想着调整自己的学习方法. 本来自己就没什么学习方法,加上老师布置下做不完的题目,况且时常听到"题海战术"的

Codeforces 442C Artem and Array(stack+贪心)

题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数,删除一个数的得分为两边数的最小值,如果左右有一边不存在则算作0分.问最大得分是多少. 解题思路:首先将连续的a,b,c,a > b && c > b的情况将c掉,获得min(a,b)分,这样处理后数组变成一个递増再递减的序列,除了最大和第二大的取不到,其他数字均可以得分. 样例:4 10 2 2 8 #include <cstdio> #include

Codeforces 18D Seller Bob java大数+贪心

题目链接:点击打开链接 java: import java.math.BigInteger; import java.util.Scanner; public class Main { static int N = 5005; static BigInteger[] er = new BigInteger[N]; static BigInteger E = new BigInteger("2"); static int[] a = new int[N]; static int[] ma

Codeforces 437C The Child and Toy(贪心)

题目连接:Codeforces 437C The Child and Toy 题目大意:孩子有一个玩具,有n个部件组成,m条绳子组成,每条绳子连接两个部件.小孩比较顽皮,要将玩具拆成不可分割的部件,每次剪断一条绳子的代价是该绳子连接的两个部件的权值中较小的值.问说最小的总代价是多少. 解题思路:以为每条边都是要被剪断的,所以将节点按照代价值从大到小排序,每次拿掉权值大的点,与该点连接并且还未剪断的边均用另外点的权值. #include <cstdio> #include <cstring