Atcoder beginner contest 336 -- D -- Pyramid

2024-01-15 04:12

本文主要是介绍Atcoder beginner contest 336 -- D -- Pyramid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

D -- Pyramid:

题目大意:

思路解析:

代码实现:


D -- Pyramid:

题目大意:

给你一个长度为n的数组,你可以对这个数组进行以下操作。

操作1:选择任意一个元素,使其值大小减一。

操作2:删除第一个或者最后一个元素。

在进行有限次操作后,一定可以得到一个形如1 2 3 2 1这样的数字金字塔,问这个长度为n的数组能得到最长的数字金字塔的长度是多少。 1 2 3 2 1 这样的数字金字塔长度为3.

思路解析:

对于数字金字塔的前半部分,ak1 ak2 ak3 ak4 ... ak,如果他前面加上ak0就不能构成数字金字塔了,如果将ak认为是这个数字金字塔的中心,那么我应该知道ak向左最远能到哪个地方。

对于数字金字塔的后半部分 ak ak+1 ak+2....am 如果在后面加上am1就不能构成数字金字塔了,如果将ak认为是这个数字金字塔的中心,那么我应该知道ak向右最远能到哪个地方。

知道了ak的左右边界后,Math.min(l,r)就是ak的总体边界,即当ak为中心时,这个数字金字塔的最大长度,如果对于n个元素每个都能得到他们作为中心时数字金字塔的最大长度,那么对于这些最大长度整体求最大,那么这个长度就是答案。

代码实现:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;public class Main {public static void main(String[] args) throws IOException {Scanner input = new Scanner(System.in);int n = input.nextInt();int[] arr = new int[n];int[] l = new int[n];int[] r = new int[n];for (int i = 0; i < n; i++) {arr[i] = input.nextInt();}l[0] = 1;for (int i = 1; i < n; i++) {l[i] = Math.min(l[i - 1] + 1, arr[i]);}r[n - 1] = 1;for (int i = n - 2; i >= 0; i--) {r[i] = Math.min(r[i + 1] + 1, arr[i]);}int ans = 0;for (int i = 0; i < n; i++) {ans = Math.max(ans, Math.min(l[i], r[i]));}System.out.println(ans);}
}

这篇关于Atcoder beginner contest 336 -- D -- Pyramid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

atcoder ABC 359-B题详解

atcoder ABC 359-B题详解 Problem Statement There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color Ai. Here, the clothes have N colors from 1

Atcoder Beginner Contest 359

传送门 A - Count Takahashi 时间限制:2秒        内存限制:1024MB 分数:100分 问题描述 给定 N 个字符串。 第 i 个字符串  () 要么是 Takahashi 要么是 Aoki。 有多少个 i 使得  等于 Takahashi ? 限制 N 是整数。每个字符串  是 Takahashi 或者 Aoki。() 输入格式

AtCoder Beginner Contest 359 A~C(D~F更新中...)

A.Count Takahashi 题意 给出 N N N个字符串,每个字符串为以下两种字符串之一: "Takahashi" "Aoki" 请你统计"Takahashi"出现了多少次。 分析 输入并统计即可。 代码 #include <bits/stdc++.h>using namespace std;typedef long long ll;void solve() {i

atcoder ABC 359-A题详解

atcoder ABC 359-A题详解 Problem Statement You are given N strings. The i-th string Si(1≤i≤N) is either Takahashi or Aoki. How many i are there such that Si is equal to Takahashi? Constraints 1≤N≤10

AtCoder ABC 365G 凸包 + 二分

题意 AtCoder ABC 365G Freestyle 题解 考虑任两种操作 ( A i , B i ) (A_{i},B_{i}) (Ai​,Bi​)和 ( A j , B j ) (A_{j},B_{j}) (Aj​,Bj​),则他们的任意组合可以表示为 ( t A i + ( 1 − t ) A j , t B i + ( 1 − t ) B j ) \big(tA_{i}+(1-

[Beginner]MySQL 主机名与权限表之间的关系

概要 在进行连接远程主机或者进行复制的时候,经常会遇到用户连接不上远程主机的错误,很烦人。常常会出现类似下面的错误:   [连接远程主机] : ERROR 1045 (28000): Access denied for user 'user'@'host' (using password: YES)   [复制中的IO]    : [ERROR] Slave I/O: error conne

atcoder ABC 358-B题详解

atcoder ABC 358-B题详解 Problem Statement At the entrance of AtCoder Land, there is a single ticket booth where visitors line up to purchase tickets one by one. The purchasing process takes A seconds p

AtCoder Beginner Contest 358 A~E(F,G更新中...)

A.Welcome to AtCoder Land 题意 给出两个字符串 S , T S, T S,T,请你判断是否满足: 字符串 S S S为AtCoder 字符串 T T T为Land 分析 输入后判断即可 代码 #include<bits/stdc++.h>using namespace std;void solve() {string s, t;cin >> s >