Educational Codeforces Round 162 (Rated for Div. 2) -- D Slimes -- 题解

2024-02-26 23:20

本文主要是介绍Educational Codeforces Round 162 (Rated for Div. 2) -- D Slimes -- 题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 D Slimes -- 题解

题目大意:

 思路解析:

        对于每个史莱姆来说,他要么被左边区间合并出来的史莱姆合并,要么被右边区间合并出来的史莱姆合并。解决这个问题我们只需要查询左边那个区间是大于他的,右边那个区间是大于他的,可以前缀和+二分搜索来解决这个问题。

        但是如果我们找到一个区间 2 2 2 2 > 7 ,但是他显然不能被这个区间合并,因为相同的史莱姆不能合并,即这个区间无法合并成一个大史莱姆,可以发现如果一个区间只要出现不想等的数字,那么一定能合并为一个大史莱姆。 (这个信息也可以用前缀和+二分维护)

        所以第一次往左边搜索,第二次往右边搜索。

代码实现:

        

import java.io.*;
import java.util.*;public class Main {static int inf = (int) 1e9;static int mod = (int) 1e9 + 7;static int mod9 = 998244353;static Vector<Integer>[] g;static char[] s;static char[] a;public static void main(String[] args) throws IOException {int T = input.nextInt();for (int o = 0; o < T; o++) {int n = input.nextInt();int[] arr = new int[n+1];long[] sum1 = new long[n+2];long[] sum2 = new long[n+2];int[] res = new int[n+1];for (int i = 1; i <= n; i++) {res[i] = n+1;arr[i] = input.nextInt();}arr[0] = arr[1];for (int i = 1; i <= n; i++) {sum1[i] = sum1[i-1] + arr[i];sum2[i] = sum2[i - 1] + (arr[i] != arr[i-1] ? 1 : 0);}for (int i = 1; i <= n; i++) {int ans = -1;int l = 1;int r = i - 1;// i - midwhile (l <= r){int mid = (l + r) >> 1;if (sum1[i-1] - sum1[mid-1] > arr[i] && ((sum2[i-1] - sum2[mid] >= 1 || arr[i-1] > arr[i]) || i - 1 == mid)){ans = mid;l = mid + 1;}else {r = mid - 1;}}if (ans != -1) res[i] = Math.min(res[i], i - ans);}for (int i = n; i >= 1; i--) {if (i == n){sum2[i] = 0;sum1[i] = arr[i];}else {sum1[i] = sum1[i+1] + arr[i];sum2[i] = sum2[i + 1] + (arr[i] != arr[i+1] ? 1 : 0);}}for (int i = 1; i <= n; i++) {int ans = -1;int l = i+1;int r = n;// i - midwhile (l <= r){int mid = (l + r) >> 1;if (sum1[i+1] - sum1[mid+1] > arr[i] && ((sum2[i+1] - sum2[mid] >= 1||arr[i+1] > arr[i]) || mid -1 == i)){ans = mid;r = mid - 1;}else {l = mid + 1;}}if (ans != -1) res[i] = Math.min(res[i], ans - i);}for (int i = 1; i <= n; i++) {if (res[i] == n+1) out.print(-1 + " ");else out.print(res[i] + " ");}out.println();}out.flush();out.close();br.close();}static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));static Input input = 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 char[] nextChars(){return next().toCharArray();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}}
}

这篇关于Educational Codeforces Round 162 (Rated for Div. 2) -- D Slimes -- 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述