思路:

先对a数组排序,然后使用动态规划。dp[i]表示前i个能否正确划分。则如果存在dp[j] == 1, i - j + 1 >= k并且a[i] - a[j] < d,那么dp[i] = 1,否则dp[i] = 0。可以使用树状数组对区间查询操作进行优化。复杂度O(n * log(n))。
实现:

 #include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
int n, k, d, a[MAXN], bit[MAXN];
inline int lowbit(int x) { return x & -x; }
void add(int i, int x)
{
while (i <= n) { bit[i] += x; i += lowbit(i); }
}
int sum(int i)
{
if (i < ) return -;
if (!i) return ;
int ans = ;
while (i) { ans += bit[i]; i -= lowbit(i); }
return ans + ;
}
int main()
{
while (cin >> n >> k >> d)
{
memset(bit, , sizeof bit);
for (int i = ; i <= n; i++) cin >> a[i];
sort(a + , a + n + );
for (int i = ; i <= n; i++)
{
int pos = lower_bound(a + , a + n + , a[i] - d) - a;
if (i - pos + < k) continue;
if (sum(i - k) - sum(pos - ) >= ) add(i, );
}
cout << (sum(n) - sum(n - ) ? "YES" : "NO") << endl;
}
return ;
}

CF985E Pencils and Boxes的更多相关文章

  1. 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 ...

  2. Codeforces 985 E - Pencils and Boxes

    E - Pencils and Boxes 思路: dp 先排个序,放进一个袋子里的显然是一段区间 定义状态:pos[i]表示小于等于i的可以作为(放进一个袋子里的)一段区间起点的离i最近的位置 显然 ...

  3. 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 ...

  4. codeforces 985E Pencils and Boxes(dp+思维)

    E. Pencils and Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. codeforces 985E Pencils and Boxes

    题意: 把一个数组分成若干组,保证每组的size >= k并且一组中任意两个数字的差的绝对值 <= d,问存不存在这样的分法. 思路: 线性dp. 用dp[i]表示前i个数是否有分法. 设 ...

  6. Codeforces985E. Pencils and Boxes (单调队列)

    题意:n个数 把他们放进一些盒子里 每个盒子最少放k个数 且最小和最大的差不能大于d 题解:显然排个序 对于当前点 存一下前面有哪些节点可以当作结尾 那么就可以枚举这些点的下一个点作为起点能否和当前点 ...

  7. CF习题集三

    CF习题集三 一.CF8C Looking for Order 题目描述 \(Lena\)喜欢秩序井然的生活.一天,她要去上大学了.突然,她发现整个房间乱糟糟的--她的手提包里的物品都散落在了地上.她 ...

  8. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  9. Codeforces 985 最短水桶分配 沙堆构造 贪心单调对列

    A B /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a, ...

随机推荐

  1. C/S转分布式数据库的解决方法

    C/S转分布式数据库的解决方法1. 直接VPN建一个网不就行了.(大概是虚拟成一个网络)2. 直连也可以,就是速度慢3. 还是三层吧,推荐RTC4. 弄个花生壳硬件试试呢,成本低,不用改程序5. 搞一 ...

  2. hrtimer高精度定时器的简单使用【学习笔记】

    #include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...

  3. eclipse安装lombok和常用注解使用

    1.下载lombok.jar lombok 的官方网址:http://projectlombok.org/   2.运行lombok.jar: java -jar  D:\eclipse-luna\l ...

  4. Pycharm中如何安装python库

    1首先打开pycharm工具,选择File中的Setting选项,如下图所示 2在打开的setting界面中我们点击python的解释器,你会看到很多导入的第三方库,如下图所示,点击最右边的加号 3在 ...

  5. 【系列】 2-SAT

    bzoj 1997 Planar 题目大意: 给一个存在曼哈顿回路的无向图,求该图是否为平面图 思路: 先把曼哈顿回路提出来,则剩下的边的两个端点若有$ABAB$的形式则这两条边必定一个在环外一个在环 ...

  6. SPOJ:Another Version of Inversion(二维数组的逆序对)

    DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradi ...

  7. AutoIt: WinGetClassList可以把当前窗口所有的handle全部列出来

    WinGetClassList 是一个非常有用的函数,可以返回当前窗口中所有的handle.返回的是一个字符串,可以使用split函数来拆分之. Opt("WinTitleMatchMode ...

  8. 【前端】Nodejs给没有引号的json数据添加引号

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/node_json.html 我们经常会遇到一些json数据需要放到json文件中,然后动态加载的场景. 但是也经常 ...

  9. vue-router 基本知识点

    vue-router就是将组件映射到路由,然后告诉vue-router在哪里渲染它们. 默认路由出口 <router-view></router-view> 使用router- ...

  10. View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations

      Animations Animations provide fluid visual transitions between different states of your user inter ...