codeforces 985E Pencils and Boxes(dp+思维)
2 seconds
256 megabytes
standard input
standard output
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a1, a2, ..., an of n integer numbers — saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
- Each pencil belongs to exactly one box;
- Each non-empty box has at least k pencils in it;
- If pencils i and j belong to the same box, then |ai - aj| ≤ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |ai - aj| ≤ d and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO".
The first line contains three integer numbers n, k and d (1 ≤ k ≤ n ≤ 5·105, 0 ≤ d ≤ 109) — the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — saturation of color of each pencil.
Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO".
- 6 3 10
- 7 2 7 7 4 2
- YES
- 6 2 3
- 4 5 3 13 4 10
- YES
- 3 2 5
- 10 16 22
- NO
In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
题意:有n个铅笔,每只铅笔有个value,现在把他们分发到几个笔筒中,每个笔筒最少放k个笔,且放入的每支笔的value差不能大于d。求解能不能找到一种方案满足。
思路:
dp。先排序,判断最后一个数能否作为一个序列的结尾。
- #include <iostream>
- #include<cstring>
- #include<string>
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- #include<deque>
- #include<vector>
- #define ll long long
- #define inf 0x3f3f3f3f
- #define mod 1000000007;
- using namespace std;
- ll a[];
- bool dp[];
- int main()
- {
- int n,k;
- ll d;
- scanf("%d%d%I64d",&n,&k,&d);
- for(int i=;i<=n;i++)
- {
- scanf("%I64d",&a[i]);
- }
- sort(a+,a++n);
- memset(dp,,sizeof(dp));
- dp[]=;
- int p=;
- for(int i=;i<=n;i++)
- {
- if(dp[i])//它是结尾的基础上,从下一个开始判断哪些可以作为结尾
- {
- p=max(p,i+k);//算过的就不用再重复算了,以防超时
- //经典样例 50000 10 0
- //1 1 1 1 1 1 1 1 1 1 1 ……
- while(p<=n&&a[p]-a[i+]<=d)
- {//如果它可以作为结尾
- dp[p]=;
- p++;
- }
- }
- }
- printf(dp[n]?"YES\n":"NO\n");
- return ;
- }
codeforces 985E Pencils and Boxes(dp+思维)的更多相关文章
- codeforces 985E Pencils and Boxes
题意: 把一个数组分成若干组,保证每组的size >= k并且一组中任意两个数字的差的绝对值 <= d,问存不存在这样的分法. 思路: 线性dp. 用dp[i]表示前i个数是否有分法. 设 ...
- codeforces 985 E. Pencils and Boxes (dp 树状数组)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- codeforces 572 D. Minimization(dp+ 思维)
题目链接:http://codeforces.com/contest/572/problem/D 题意:给出一个序列,可以任意调整序列的顺序,使得给出的式子的值最小 题解:显然要先排一下序,然后取相邻 ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Codeforces 985 E - Pencils and Boxes
E - Pencils and Boxes 思路: dp 先排个序,放进一个袋子里的显然是一段区间 定义状态:pos[i]表示小于等于i的可以作为(放进一个袋子里的)一段区间起点的离i最近的位置 显然 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces 407B Long Path(好题 DP+思维)
题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...
随机推荐
- RabbitMQ 简单了解以及使用
RabbitMQ 开发语言:Erlang – 面向并发的编程语言. AMQP:是消息队列的一个协议. mysql 是 java 写的吗?不是 那么 java 能不能访问?可以,则通过(驱动)协议;那么 ...
- AtCoder ARC097C Sorted and Sorted:dp
传送门 题意 有 $ 2n $ 个球排成一行,其中恰好有 $ n $ 个白球和 $ n $ 个黑球.每个球上写着数字,其中白球上的数字的并集为 $ \lbrace 1 \dots n\rbrace $ ...
- hibernate学习(4)
Hibernate查询方式 1 对象导航查询 (1)根据id查询某个客户,再查询这个客户里面所有的联系人 2 OID查询 (1)根据id查询某一条记录,返回对象 3 hql查询 (1)Query对象, ...
- python学习笔记(pict+subprocess)
这几天看到接口自动化用例的生成,关于这里博主自己也想了想,是否可以根据参数的范围自动生成用例,这样就不用一条一条的写接口测试用例 这里就设计到用例设计的方法,让我想到之前接触过一款微软的用例自动生成工 ...
- SSL和SSH的区别
SSL是一种国际标准的加密及身份认证通信协议,您用的浏览器就支持此协议.SSL(Secure Sockets Layer)最初是由美国Netscape公司研究出来的,后来成为了Internet网上安全 ...
- MVVM3
MVVM设计模式 2010-09-19 23:59:18| 分类: MVVM | 标签:mvvm silverlight4 mvc mvp command |举报|字号 订阅 一 ...
- 5.6 WebDriver API实例讲解(41-50)
41.操作Web页面的滚动条 (1)滑动页面的滚动条到页面的最下面. (2)滑动页面的滚动条到页面的某个元素. (3)滑动页面的滚动条向下移动某个数量的像素. package apiSample; i ...
- mysql数据库(三):查询的其他用法
一. 查询—IN的用法 语法:select ... from 表名 where 字段 a in (值b, 值c, 值d...) 等价于 select ... from 表名 where 字段a=值b ...
- [MYSQL]时间毫秒数转换
java中常用bigint字段保存时间,通常将时间保存为一大串数字,每次取出需要在程序里转换,有时候程序里不方便,可以使用MYSQL自带的函数FROM_UNIXTIME(unix_timestamp, ...
- position值详情
1.absolute属性: 英文直译成汉语的解释有:绝对,独立的.显然在做为css中position属性的值时“绝对”这个意思更恰当一些.他的作用是:生成绝对定位的元素,相对于static 定位以外的 ...