Codeforces 985 E - Pencils and Boxes
思路:
dp
先排个序,放进一个袋子里的显然是一段区间
定义状态:pos[i]表示小于等于i的可以作为(放进一个袋子里的)一段区间起点的离i最近的位置
显然,初始状态:pos[i] = 1,1 <= i <= k
状态转移:
pos[i+1] = i+1 ,如果 a[i] - a[pos[i-k+1]] <= d , 因为在这种情况下pos[i-k+1] 到 i 可以放进一个袋子里,那么i+1就可以作为新的起点了
pos[i+1] = pos[i], 其他情况
最后只要判断pos[n+1] 等不等于 n+1 就可以判断是不是都能放进袋子里
代码:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 5e5 + ;
int a[N], pos[N];
int main() {
int n, k, d;
scanf("%d%d%d", &n, &k, &d);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
sort(a+, a++n);
for (int i = ; i <= k; i++) pos[i] = ;
for (int i = k; i <= n; i++) {
int pre = pos[i-k+];
if(a[i] - a[pre] <= d) pos[i+] = i+;
else pos[i+] = pos[i];
//cout << i+1 << " " << pos[i+1] << endl;
}
if(pos[n+] == n+) printf("YES\n");
else printf("NO\n");
return ;
}
Codeforces 985 E - Pencils and Boxes的更多相关文章
- 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 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 985E Pencils and Boxes(dp+思维)
E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 985 最短水桶分配 沙堆构造 贪心单调对列
A B /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a, ...
- codeforces 985E Pencils and Boxes
题意: 把一个数组分成若干组,保证每组的size >= k并且一组中任意两个数字的差的绝对值 <= d,问存不存在这样的分法. 思路: 线性dp. 用dp[i]表示前i个数是否有分法. 设 ...
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 768F】 Barrels and boxes
http://codeforces.com/problemset/problem/768/F (题目链接) 题意 A,B两种物品可以装到栈中,每个栈只能存放一种物品,容量没有限制.现在讲所有栈排成一列 ...
- codeforces 551 C GukiZ hates Boxes
--睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
- 【codeforces 768F】Barrels and boxes
[题目链接]:http://codeforces.com/problemset/problem/768/F [题意] 让你把f个food和w个wine装在若干个栈里面; 每个栈只能装food或者是wi ...
随机推荐
- php操作共享内存shmop类及简单使用测试(代码)
SimpleSHM 是一个较小的抽象层,用于使用 PHP 操作共享内存,支持以一种面向对象的方式轻松操作内存段.在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码.可以使用 3 ...
- 13: openpyxl 读写 xlsx文件
1.1 openpyxl 基本使用 1.openpyxl 将xlsx读成json格式 #! /usr/bin/env python # -*- coding: utf-8 -*- # -*- codi ...
- C++进程间通信之剪贴板
转载:http://blog.csdn.net/hk627989388/article/details/53693636 转载:http://blog.csdn.net/heihei36/articl ...
- python --- 18 类与类之间的关系, 特殊成员
类与类之间的关系 一.依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作 随时可以更换另外一个东西来完成此操作 此时的关系是最轻的. 二.关联关系(组合关系,聚合关系) 1.一对 ...
- Web Service平台有三种元素构成:SOAP、WSDL、UDDI。区别和联系
Web Service平台有三种元素构成:SOAP.WSDL.UDDI.一个消费者可以在UDDI注册表查找服务,取得服务的WSDL描述,然后通过SOAP来调用服务.SOAP.WSDL.UDDI的区别如 ...
- linux内核中的dquot是什么?
答:这个与磁盘配额管理(disk quota)有关,内核配置选项为CONFIG_QUOTA,使能此选项意味着可以设置每个用户的硬盘使用限制.
- 关于 RabbitMQ 的 Dead-Letters-Queue “死信队列”
来自一个队列的消息可以被当做‘死信’,即被重新发布到另外一个“exchange”去,这样的情况有: 消息被拒绝 (basic.reject or basic.nack) 且带 requeue=fa ...
- c# 之 事务
SqlTransaction事务的简单例子 { DataTable dt = new DataTable(); System.Data.SqlClient.SqlConnection cnn = ne ...
- P4091 [HEOI2016/TJOI2016]求和
留待警戒 FFT的时候长度要写的和函数里一样啊XD 瞎扯 这是个第二类斯特林数的理性愉悦颓柿子题目 颓柿子真的是让我hi到不行啦(才没有) 前置芝士 一个公式 \[ \sum_{i=0}^n t^i ...
- mysql分区分表讲解
为什么要分表和分区? 日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询的情况,性能 ...