zoukankan      html  css  js  c++  java
  • 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解

    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 the barn.

    Since Bessie wants the diamonds in each of the two cases to be relatively similar in size, she decides that she will not include two diamonds in the same case if their sizes differ by more than KK (two diamonds can be displayed together in the same case if their sizes differ by exactly KK). Given KK, please help Bessie determine the maximum number of diamonds she can display in both cases together.

    奶牛Bessie很喜欢闪亮亮的东西(BalingBaling),所以她喜欢在她的空余时间开采钻石!她现在已经收集了N颗不同大小的钻石(N<=50,000),现在她想在谷仓的两个陈列架上摆放一些钻石。

    Bessie想让这些陈列架上的钻石保持相似的大小,所以她不会把两个大小相差K以上的钻石同时放在一个陈列架上(如果两颗钻石的大小差值为K,那么它们可以同时放在一个陈列架上)。现在给出K,请你帮Bessie确定她最多一共可以放多少颗钻石在这两个陈列架上。

    输入格式

    The first line of the input file contains (N) and (K) ((0 leq K leq 1,000,000,000)).

    The next (N) lines each contain an integer giving the size of one of the

    diamonds. All sizes will be positive and will not exceed (1,000,000,000).

    输出格式

    Output a single positive integer, telling the maximum number of diamonds that

    Bessie can showcase in total in both the cases.

    输入输出样例

    输入 #1

    7 3
    10
    5
    1
    12
    9
    5
    14

    输出 #1

    5

    【思路】

    排序 + 双指针

    【前缀思想】

    很有意思的一道题目
    这个需要找两组差不超过k的并且和最大的序列
    因为需要差不超过k,
    所以可以先将序列排一下序
    然后就成为了有序的序列(很显然对吧)
    这样就可以用双指针记录头和尾
    然后只要头和尾的差小于等于k
    就可以尝试一下这个区间能不能再扩大
    所以可以很简单的就用双指针找出最长的差小于等于k的序列

    【中心思想】

    但是,这道题目有两个陈列架
    所以找出一个最大的是不行的
    而且找出次大的和最大的加起来也是不可行的
    因为不保证最大和次大的区间不重叠
    所以可以正序扫一遍然后找出
    到每一个点之前最长的差小于等于k的序列
    然后再倒序找一遍找出
    到每一个点之后最长的差小于等于k的序列

    【小细节】

    这样,只需要再枚举一遍每一个点
    然后比较前后加起来的和最大的就是了
    不过,如果出现了前后最大的刚好重叠在枚举到的这个点
    那么加起来的和就会重复一次
    而且是不合法的
    毕竟你不能把一个宝石放在两个陈列架上
    所以就有了后面在比较时候的:
    M = max(M,qian[i] + hou[i + 1]);
    这样就不会出现一个点刚好是两个区间的交点的情况了

    【完整代码】

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    
    using namespace std;
    const int Max = 50005;
    int a[Max];
    int qian[Max];
    int hou[Max];
    int main()
    {
    	int n,k;
    	cin >> n >> k;
    	for(register int i = 1;i <= n;++ i)
    		cin >> a[i];
    	sort(a + 1,a + 1 + n);
    	for(register int l = 1,r = 1;r <= n;++ r)
    	{
    		while(a[r] - a[l] > k && l <= r)l ++;
    		qian[r] = max(qian[r - 1],r - l + 1);
    	}
    	for(register int r = n,l = n;l >= 1;l --)
    	{
    		while(a[r] - a[l] > k && l <= r)r --;
    		hou[l] = max(hou[l] + 1,r - l + 1);
    	}
    	int M = 0;
    	for(register int i = 1;i < n;++ i)
    		M = max(M,qian[i] + hou[i + 1]);
    	cout << M << endl;
    	return 0;
    }
    
  • 相关阅读:
    51nod1381 硬币游戏
    51nod1381 硬币游戏
    51nod1384 全排列
    LOJ P10130 点的距离 题解
    POJ P1985 Cow Marathon 题解
    求树的直径(两种方法)
    洛谷 P3518 [POI2011] SEJ-Strongbox 题解
    洛谷 UVA12101 Prime Path 题解
    POJ P2251 Dungeon Master 题解
    POJ P3009 Curling 2.0 题解
  • 原文地址:https://www.cnblogs.com/acioi/p/11646404.html
Copyright © 2011-2022 走看看