(AtCoder Beginner Contest 327) --- F-Apples --- 题解 (一个比较难想的线段树题)

2024-03-22 10:20

本文主要是介绍(AtCoder Beginner Contest 327) --- F-Apples --- 题解 (一个比较难想的线段树题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

F-Apples:

题目大意:

​编辑​编辑 思路解析:

代码实现:


 

F-Apples:

题目大意:

样例解释:

 思路解析:

 题目要求我们选择任意一对S,L,让苹果满足 S-0.5<= T<= S + D - 0.5 和 L-0.5 <= X <= L + W -0.5的苹果数量尽可能多,并且输出在能选择的可能性中最多的苹果数量为多少,其实我们可以发现这个需要满足的条件其实等价于   S <= T <= S+D 和 L <= x <= L + W,这是因为T和X都是整数。

那我们可以将所有苹果按照T排序,那么我们可以利用双指针来固定任意一个S的可能性,那么我们就需要查询在当前时间轴下,怎么选择L,可以使得答案最优。这个寻优过程其实可以反过来看作每个苹果可以对W大小的区间产生影响,我们应该找到那个点拥有的影响最大,这里那么就可以使用线段树的区间修改 (查询信息的正确 其实可以在修改时就可以维护出来)。

代码实现:

import java.io.*;
import java.math.BigInteger;
import java.util.*;public class Main {static long inf = (long) 2e18;static long mod = 998244353;public static void main(String[] args) throws IOException {int t = 1;while (t > 0) {solve();t--;}w.flush();w.close();br.close();}public static void solve() {int N = f.nextInt();int D = f.nextInt();int W = f.nextInt();int max = 0;int[][] p = new int[N][2];for (int i = 0; i < N; i++) {p[i][0] = f.nextInt();p[i][1] = f.nextInt();max = Math.max(p[i][1], max);}Arrays.sort(p, ((o1, o2) -> {return o1[0] - o2[0];}));int i = 0;int j = 0;SegTree seg = new SegTree();seg.build(1, 1, max);int ans = 0;while (j < N){if (i > 0){int x = p[i-1][1];seg.add(1, Math.max(1, x - W + 1), x, -1);}while (j < N && p[j][0] - p[i][0] < D){int x = p[j][1];seg.add(1, Math.max(1, x - W + 1), x, 1);j++;}ans = Math.max(ans, seg.t[1].max);i++;}w.println(ans);}static int MAXN = (int) 2e5 + 5;static class Node{int l, r, max, lazy;}static class SegTree{Node[] t = new Node[MAXN * 4];public SegTree(){for (int i = 0; i < MAXN * 4; i++) {t[i] = new Node();}}public void build(int root, int l, int r){t[root].l = l; t[root].r = r;if (l == r) return;int mid = (l + r) >> 1;build(root << 1, l, mid);build((root << 1) | 1, mid+1, r);}public void push_down(int root){if (t[root].lazy != 0){if (t[root].l != t[root].r){int ch = root << 1;int x = t[root].lazy;t[ch].max += x;t[ch |1].max += x;t[ch].lazy += x;t[ch | 1].lazy += x;}t[root].lazy = 0;}}public void add(int root, int l, int r, int x){push_down(root);if (t[root].l == l && t[root].r == r){t[root].max += x;t[root].lazy += x;return;}int mid = (t[root].l + t[root].r) >> 1;int ch = root << 1;if (r <= mid) add(ch, l, r, x);else if (l > mid) add(ch|1, l, r, x);else {add(ch, l, mid, x);add(ch|1, mid+1, r, x);}update(root);}public void update(int root){t[root].max = Math.max(t[root << 1].max, t[(root << 1) | 1].max);}}static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));static Input f = new Input(System.in);static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static class Input {public BufferedReader reader;public StringTokenizer tokenizer;public Input(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}public String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) {try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();}public String nextLine() {String str = null;try {str = reader.readLine();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}return str;}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public Double nextDouble() {return Double.parseDouble(next());}public BigInteger nextBigInteger() {return new BigInteger(next());}}
}

这篇关于(AtCoder Beginner Contest 327) --- F-Apples --- 题解 (一个比较难想的线段树题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/835110

相关文章

百度/小米/滴滴/京东,中台架构比较

小米中台建设实践 01 小米的三大中台建设:业务+数据+技术 业务中台--从业务说起 在中台建设中,需要规范化的服务接口、一致整合化的数据、容器化的技术组件以及弹性的基础设施。并结合业务情况,判定是否真的需要中台。 小米参考了业界优秀的案例包括移动中台、数据中台、业务中台、技术中台等,再结合其业务发展历程及业务现状,整理了中台架构的核心方法论,一是企业如何共享服务,二是如何为业务提供便利。

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin