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 ...
随机推荐
- Spring Boot(九):定时任务
Spring Boot(九):定时任务 一.pom包配置 pom包里面只需要引入springboot starter包即可 <dependencies> <dependency> ...
- 我仅使用到的dd if
备份一个分区 分区 镜像名 读写块大小 dd if=/dev/sdb of=/diskone.img bs=512 注:可以为了提升I/O把bs设为较高的数值例:bs=1024k 挂载一个分区 mou ...
- 线性回归、Logistic回归、Softmax回归
线性回归(Linear Regression) 什么是回归? 给定一些数据,{(x1,y1),(x2,y2)…(xn,yn) },x的值来预测y的值,通常地,y的值是连续的就是回归问题,y的值是离散的 ...
- topcoder srm 686 div1
problem1 link 左括号和右括号较少的一种不会大于20.假设左括号少.设$f[i][mask][k]$表示处理了前$i$个字符,其中留下的字符以$k$开头($k=0$表示'(',$k=1$表 ...
- SpringBoot 解决HttpServletRequest只能读取一次
业务逻辑,通过filter读取请求的request,获取token,并将token传递后面流程使用 BodyReaderHttpServletRequestWrapper: public class ...
- Why there is two completely different version of Reverse for List and IEnumerable?
https://stackoverflow.com/questions/12390971/why-there-is-two-completely-different-version-of-revers ...
- 【#和$】MyBatis中#和$的区别
一.结论 #{}:占位符号,好处防止sql注入 ${}:sql拼接符号 二.具体分析 动态 SQL 是 mybatis 的强大特性之一,也是它优于其他 ORM 框架的一个重要原因.mybatis 在对 ...
- c# 之 System.Type.GetType()与Object.GetType()与typeof比较
Object.GetType()与typeof的区别 //运算符,获得某一类型的 System.Type 对象. Type t = typeof(int); //方法,获取当前实例的类型. ; Con ...
- 题解——牛客网OI赛制测试赛2
T1 规律题 考虑先全部选中再去重即可 #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- (转)Autonomous_Vehicle_Paper_Reading_List
Autonomous_Vehicle_Paper_Reading_List 2018-07-19 10:40:08 Reference:https://github.com/ZRZheng/Auton ...