Educational Codeforces Round 44 (Rated for Div. 2)+E. Pencils and Boxes+树状数组

题目链接:E. Pencils and Boxes

题意:N 个数,要求分成任意堆,要求每一堆只要有K个,同一堆中任意数之间差值不能超过d;

题解:用树状数组。排一下序然后从后面开始找,以当前数为最小值看能否成堆,要成堆的话要求i+k,到第一个大于a[i]+d的位置之间有能够成堆的位置。(这里判断的时候树状数组一减看是否大于0就可以了)注意初始化时在n+1的位置加1.

 1 #include<bits/stdc++.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <stdio.h>
 6 #include <math.h>
 7 #define ll long long
 8 #define pb push_back
 9 using namespace std;
10 const int N=800010;
11 const int INF=1<<30;
12 const int inf=0x3f3f3f3f;
13 const int maxn=5e5+10;
14 const int mod=1e9+7;
15 int n,k,d;
16 int lowbit(int x)
17 {
18     return x&(-x);
19 }
20 int a[maxn],b[maxn];
21 bool v[maxn];
22 int add(int x)
23 {
24     while(x<=n+1)
25     {
26         b[x]++;x+=lowbit(x);
27     }
28 }
29 int get(int x)
30 {
31     int ans=0;
32     while(x>0)
33     {
34         ans+=b[x];x-=lowbit(x);
35     }
36     return ans;
37 }
38 int main()
39 {
40     scanf("%d %d %d ",&n,&k,&d);
41     for(int i=1;i<=n;i++)
42     {
43         scanf("%d",&a[i]);
44     }
45     sort(a+1,a+1+n);
46     add(n+1);
47     for(int i=n-k+1;i>=1;i--)
48     {
49         int pos=upper_bound(a+1,a+1+n,a[i]+d)-a;
50         if(pos>=i+k-1&&get(pos)-get(i+k-1)>0)
51         {
52             v[i]=true;add(i);
53         }
54     }
55     if(v[1])printf("YES\n");
56     else printf("NO\n");
57     return 0;
58 }

原文地址:https://www.cnblogs.com/lhclqslove/p/9080062.html

时间: 2024-11-08 13:11:12

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

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)

这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[300007][10]; 5 int b[300007][10]; 6 int n,m; 7 int ans,ans2; 8 int t

Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并

题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度复杂度O(nlogn) //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm