bzoj4582[Usaco2016 Open]Diamond Collector

bzoj4582[Usaco2016 Open]Diamond Collector

题意:

n个钻石,每个都有一个大小,现在将其装进2个盒子里,每个盒子里的钻石最大的与最小的大小不能超过k,问最多能装多少个。n最大50000。

题解:

我真傻,真的~首先对大小排序,然后找以i为左端点的可装区间,这个操作两个指针就可以搞,我却以为要二分查找。预处理完了,因为不交错的区间肯定比交错的区间优,所以从n到1递推一下从n到i最大的区间大小是多少,然后枚举每个区间,找到当前区间大小加上右端点+1到n中最大的区间大小中的最大值输出即可。我却以为要找与最大区间不交错的第二大区间,结果WA了好几发,才发现这是错误的贪心QAQ

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define dec(i,j,k) for(int i=j;i>=k;i--)
 6 #define maxn 60000
 7 using namespace std;
 8
 9 int n,k,sz[maxn],r,cnt[maxn],mx[maxn];
10 int main(){
11     scanf("%d%d",&n,&k); inc(i,1,n)scanf("%d",&sz[i]); sort(sz+1,sz+n+1); r=1;
12     inc(i,1,n){
13         while(r<=n&&sz[r]-sz[i]<=k)r++; cnt[i]=r-i;
14     }
15     mx[n+1]=0; dec(i,n,1)mx[i]=max(mx[i+1],cnt[i]);
16     int ans=0; inc(i,1,n)ans=max(ans,cnt[i]+mx[i+cnt[i]]); printf("%d",ans);
17     return 0;
18 }

20160519

时间: 2024-11-11 03:26:30

bzoj4582[Usaco2016 Open]Diamond Collector的相关文章

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

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

BZOJ 4582: [Usaco2016 Open]Diamond Collector

Descrirption 给你一个长度为 \(n\) 的序列,求将它分成两个序列后最多个数,每个序列最大值最小值不能超过 \(k\) Sol 二分+DP. 排一下序,找出以这个点结尾和开始的位置. 这个玩意可以二分也可以用单调队列,随便搞啊... 然后统计答案就是枚举第二个序列的起点,然后往后扫的时候统计一下,第一个序列的最大长度就可以了. Code /************************************************************** Problem:

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

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

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

P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题意要注意一点:有两个陈列架! 如果只有一个陈列架,是很容易的.two-pointers直接从左到右跑一下即可. 如果有两个陈列架,就需要进行答案合并了.做法是这样的: 设两个数组: \(pre\)数组,\(pre[i]\)表示以\([1,i]\)为右端点时区间长度的最大值. \(suf\)数组,\(suf[i]\)表示以\([i,n]\)为左端点时区间长度的最大值. 两个数组分别是前缀最大值和后缀最大值.同样利

洛谷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