P3143 [USACO16OPEN]钻石收藏家Diamond Collector[two-pointers]

P3143 [USACO16OPEN]钻石收藏家Diamond Collector

题意要注意一点:有两个陈列架!

如果只有一个陈列架,是很容易的。two-pointers直接从左到右跑一下即可。

如果有两个陈列架,就需要进行答案合并了。做法是这样的:

设两个数组:

  1. \(pre\)数组,\(pre[i]\)表示以\([1,i]\)为右端点时区间长度的最大值。
  2. \(suf\)数组,\(suf[i]\)表示以\([i,n]\)为左端点时区间长度的最大值。

两个数组分别是前缀最大值和后缀最大值。同样利用一个陈列架的two-pointers的做法。

最后的答案就是\(max_{i=1}^{n+1} pre[i-1] + suf[i]\)。这里的\(i\)相当于拼接点。

代码:

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
#define ll long long
const int maxn = 50005;
int a[maxn];
int pre[maxn], suf[maxn];
int n, k;

void work1() {
    for(int r = 1, l = 0; r <= n; r++) {
        while(a[r] - a[l + 1] > k) l++;
        pre[r] = std::max(pre[r - 1], r - l);
    }
}
void work2() {
    for(int l = n, r = n + 1; l >= 1; l--) {
        while(a[r - 1] - a[l] > k) r--;
        suf[l] = std::max(suf[l + 1], r - l);
    }
}
int main() {
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    std::sort(a + 1, a + n + 1);
    work1();
    work2();
    int ans = 0;
    for(int i = 1; i <= n + 1; i++) {
        ans = std::max(ans, pre[i - 1] + suf[i]);
    }
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/Garen-Wang/p/10381458.html

时间: 2024-08-28 19:50:30

P3143 [USACO16OPEN]钻石收藏家Diamond Collector[two-pointers]的相关文章

洛谷P3143 [USACO16OPEN]钻石收藏家Diamond Collector

题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected  diamonds () of varying sizes, and she wants to arrange some of them in a pair of display cases in the barn. Since Bessie

洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector

题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected NN diamonds (N \leq 50,000N≤50,000) of varying sizes, and she wants to arrange some of them in a pair of display cases in

Luogu P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

又是一个学数据结构学傻了的人 才不会承认是看到Splay,觉得可以写平衡树才进来的呢 Description: 给出一个序列,问排序后,选取两个区间,使其没有重合部分,且每个区间右端点减去左端点不大于k,求这两个区间长度之和的最大值. 前置技能:FHQ-Treap.线段树 (不会的出门百度) Solution: 看数据范围,5e4,很好,O(nlogn)完全可以水过去.那么我们可以放心的考虑FHQ了.因为要最优,所以我们要找到每一个点,在有序序列中对应的满足题目条件的区间的大小,这就可以用FHQ

bzoj4582[Usaco2016 Open]Diamond Collector

bzoj4582[Usaco2016 Open]Diamond Collector 题意: n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装多少个.n最大50000. 题解: 我真傻,真的~首先对大小排序,然后找以i为左端点的可装区间,这个操作两个指针就可以搞,我却以为要二分查找.预处理完了,因为不交错的区间肯定比交错的区间优,所以从n到1递推一下从n到i最大的区间大小是多少,然后枚举每个区间,找到当前区间大小加上右端点+1到n中最大的

Diamond Collector (动态规划)

问题 I: Diamond Collector 时间限制: 1 Sec  内存限制: 64 MB提交: 22  解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected N diamonds (N≤50,000) of varying sizes, and sh

4582: [Usaco2016 Open]Diamond Collector

4582: [Usaco2016 Open]Diamond Collector Description Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected N diamonds (N≤50,000) of varying sizes, and she wants to arrange some of t

Diamond Collector

Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected N diamonds (N≤50,000) of varying sizes, and she wants to arrange some of them in a pair of display case

Bzoj 4582 [Usaco2016 Open] Diamond Collector 题解

4582: [Usaco2016 Open]Diamond Collector Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 136[Submit][Status][Discuss] Description Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! Sh

java9新特性-8-语法改进:钻石操作符(Diamond Operator)使用升级

1.使用说明 我们将能够与匿名实现类共同使用钻石操作符(diamond operator) 在java8中如下的操作是会报错的: 编译报错信息:'<>' cannot be used with anonymous classes 2.使用举例 原文地址:https://www.cnblogs.com/wzlbigdata/p/8278381.html