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 ...
随机推荐
- NATS—基础介绍
1. 介绍 NATS(Message bus): 从CloudFoundry的总架构图看,位于各模块中心位置的是一个叫nats的组件.NATS是由CloudFoundry的架构师Derek开发的一个开 ...
- 面试必问:Spring循环依赖的三种方式
引言:循环依赖就是N个类中循环嵌套引用,如果在日常开发中我们用new 对象的方式发生这种循环依赖的话程序会在运行时一直循环调用,直至内存溢出报错.下面说一下Spring是如果解决循环依赖的. 第一种: ...
- 10: vue-router和单文件组件
1.1 vue-router路由基本使用 官网: https://router.vuejs.org/zh/api/#router-link 1.安装vue-router bower info vue- ...
- 对客户端攻击:adobe_toolbutton
对客户端攻击:adobe_toolbutton 漏洞简介 远程攻击者可以利用漏洞执行任意指令.当用户打开特定pdf就可以触发payload. 实践过程 输入命令use exploit/windows/ ...
- Python3基础 list [] 创建整数列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux内核中的两个标记GFP_KERNEL和GFP_ATOMIC是用来干什么的?
1. 作用 用来标记分配内核空间内存时的方式 2. 两个标记使用在什么场合? 如果内存不够时,会等待内核释放内存,直到可以分配相应大小的内存,也就意味着会发生阻塞,因此不能使用在中断处理函数中,而GF ...
- Netty实践与NIO原理
一.阻塞IO与非阻塞IO Linux网络IO模型(5种) (1)阻塞IO模型 所有文件操作都是阻塞的,以套接字接口为例,在进程空间中调用recvfrom,系统调用直到数据包到达且被复制到应用进程缓冲区 ...
- 【Hadoop 分布式部署 二:分布式环境预备工作(主机名 IP地址等设置)】
1.首先使用工具连接上 这三台虚拟主机 2.配置主机名 切换到 root 用户 第一种方式 可以使用命令 hostname [要更改的主机名] 但是这种更改主机名的方式 ...
- 并发编程之IO模型
一.阻塞IO(blocking IO) from concurrent.futures import ThreadPoolExecutor import socket server = socket. ...
- 模块、包及常用模块(time/random/os/sys/shutil)
一.模块 模块的本质就是一个.py 文件. 导入和调用模块: import module from module import xx from module.xx.xx import xx as re ...