Git Product home page Git Product logo

Comments (1)

zwwhdls avatar zwwhdls commented on July 29, 2024

B 题

题意:有 n 个柱子,每个柱子上有一个半径为 ai 的圆盘,要求按从大到小的顺序叠起来;有以下几点限制:
1. 圆盘移动到另一个圆盘上后,不能再被移走;
2. 圆盘只能向两边移动;
如果能叠起来,输出 YES;如果不能,输出 NO

思路:先找到半径最大的圆盘,记录下该圆盘半径为 max,开始向两边遍历,依次看两边的数是否比 max 小,如果是,将 max 置为该值,再向两边移动。一旦遇到比 max 大的,输出 NO;若全部遍历完,输出 YES.

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	input := bufio.NewReader(os.Stdin)
	var num int

	_, err := fmt.Fscanf(input, "%d\n", &num)
	if err != nil {
	}

	var pillar = make([]int, num)
	var index, max int

	for i := 0; i < num; i++ {
		_, err := fmt.Fscanf(input, "%v", &pillar[i])
		if pillar[i] > max {
			max = pillar[i]
			index = i
		}
		if err != nil {
		}
	}

	for i, j := 1, 1; ; {
		left := 0
		right := 0
		if index-i >= 0 {
			left = pillar[index-i]
		}
		if index+j < num {
			right = pillar[index+j]
		}
		if index-i < 0 && index+j >= num {
			break
		}

		if left < max && left >= right {
			max = left
			i++
			continue
		}
		if right < max && right > left {
			max = right
			j++
			continue
		}
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")

}

from arts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.