Flower garden

本文介绍了一个算法,用于解决如何在花园中合理安排不同高度、开花和枯萎时间的花卉种植顺序的问题,确保高矮植物互不遮挡且美观。

Problem Statement

You are planting a flower garden with bulbs to give you joyous flowers throughout the year. However, you wish to plant the flowers such that they do not block other flowers while they are visible.

You will be given a int[] height, a int[] bloom, and a int[] wilt. Each type of flower is represented by the element at the same index of height, bloom, and wilt. height represents how high each type of flower grows, bloom represents the morning that each type of flower springs from the ground, and wilt represents the evening that each type of flower shrivels up and dies. Each element in bloom and wilt will be a number between 1 and 365 inclusive, and wilt[i] will always be greater than bloom[i]. You must plant all of the flowers of the same type in a single row for appearance, and you also want to have the tallest flowers as far forward as possible. However, if a flower type is taller than another type, and both types can be out of the ground at the same time, the shorter flower must be planted in front of the taller flower to prevent blocking. A flower blooms in the morning, and wilts in the evening, so even if one flower is blooming on the same day another flower is wilting, one can block the other.

You should return a int[] which contains the elements of height in the order you should plant your flowers to acheive the above goals. The front of the garden is represented by the first element in your return value, and is where you view the garden from. The elements of height will all be unique, so there will always be a well-defined ordering.

Definition

Class: FlowerGarden
Method: getOrdering
Parameters: int[], int[], int[]
Returns: int[]
Method signature: int[] getOrdering(int[] height, int[] bloom, int[] wilt)
(be sure your method is public)

Constraints
- height will have between 2 and 50 elements, inclusive.
- bloom will have the same number of elements as height
- wilt will have the same number of elements as height
- height will have no repeated elements.
- Each element of height will be between 1 and 1000, inclusive.
- Each element of bloom will be between 1 and 365, inclusive.
- Each element of wilt will be between 1 and 365, inclusive.
- For each element i of bloom and wilt, wilt[i] will be greater than bloom[i].

Examples
0)

{5,4,3,2,1}
{1,1,1,1,1}
{365,365,365,365,365}
Returns: { 1, 2, 3, 4, 5 }
These flowers all bloom on January 1st and wilt on December 31st. Since they all may block each other, you must order them from shortest to tallest.
1)

{5,4,3,2,1}
{1,5,10,15,20}
{4,9,14,19,24}
Returns: { 5, 4, 3, 2, 1 }
The same set of flowers now bloom all at separate times. Since they will never block each other, you can order them from tallest to shortest to get the tallest ones as far forward as possible.
2)

{5,4,3,2,1}
{1,5,10,15,20}
{5,10,15,20,25}
Returns: { 1, 2, 3, 4, 5 }
Although each flower only blocks at most one other, they all must be ordered from shortest to tallest to prevent any blocking from occurring.
3)

{5,4,3,2,1}
{1,5,10,15,20}
{5,10,14,20,25}
Returns: { 3, 4, 5, 1, 2 }
The difference here is that the third type of flower wilts one day earlier than the blooming of the fourth flower. Therefore, we can put the flowers of height 3 first, then the flowers of height 4, then height 5, and finally the flowers of height 1 and 2. Note that we could have also ordered them with height 1 first, but this does not result in the maximum possible height being first in the garden.
4)

{1,2,3,4,5,6}
{1,3,1,3,1,3}
{2,4,2,4,2,4}
Returns: { 2, 4, 6, 1, 3, 5 }
5)

{3,2,5,4}
{1,2,11,10}
{4,3,12,13}
Returns: { 4, 5, 2, 3 }

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ll __int64
#define REP(i,n) for(int i=0;i<(n);i++)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
int res[55],h[55],s[55],e[55],use[55],n;
void ans()
{
    REP(i,n){
            int best=-1;
        REP(j,n)if(!use[j]){
                int ok=1;
            REP(z,n)
            if( j !=z && !use[z] && 
            s[z] <= e[j] && e[z] >= s[j] && h[z] < h[j])ok=0;/*满足任意其中任意条件就可以选,然后在下面选出一个最佳的*/
            if(ok&&(best==-1||h[best] < h[j]))best=j;
        }
        res[i]=h[best];
        use[best]=1;
    }
    REP(i,n)printf(i?" %d":"%d",res[i]);
}
int main(){
    scanf("%d",&n);
    REP(i,n)scanf("%d",&h[i]);
    REP(i,n)scanf("%d",&s[i]);
    REP(i,n)scanf("%d",&e[i]);
    ans();
}
//Name of Exercise // planter.c // // This program was written by z5660101 (zXXXXXXX) // on 2025/10/19 // // This program simulates a garden where a gardener plants flowers (Daisy, Rose, Tulip) in a 5x5 grid. Flowers grow automatically even during the planting process, and take 3 rounds to bloom. The garden state is printed after each operation. #include <stdio.h> #define GARDEN_ROWS 5 #define GARDEN_COLS 5 #define TRUE 1 #define FALSE 0 /* Provided enum flowers DO NOT MODIFY THIS ENUM */ enum flowers { EMPTY, DAISY, ROSE, TULIP }; /* Provided struct garden DO NOT MODIFY THIS STRUCT */ struct garden { enum flowers flower; int bloom_countdown; int is_gardener; }; /* provided function prototypes DO NOT MODIFY THESE FUNCTION PROTOTYPES */ void print_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]); int grow_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]); int main(void) { printf("Welcome to the planter!\n"); struct garden garden[GARDEN_ROWS][GARDEN_COLS] = {0}; for (int i = 0; i < GARDEN_ROWS; i++) { for (int j = 0; j < GARDEN_COLS; j++) { garden[i][j].flower = EMPTY; garden[i][j].bloom_countdown = 0; garden[i][j].is_gardener = FALSE; } } for (int row = 0; row < GARDEN_ROWS; row++) { garden[row][0].is_gardener = TRUE; print_garden(garden); printf( "0. Nothing\n" "1. Daisy\n" "2. Rose\n" "3. Tulip\n" "Which flower would you like to plant in this row? " ); int choice; scanf("%d", &choice); for (int col = 0; col < GARDEN_COLS; col++) { garden[row][col].is_gardener = FALSE; if (choice == 0) { garden[row][col].flower = EMPTY; garden[row][col].bloom_countdown = 0; } else if (choice == 1) { garden[row][col].flower = DAISY; garden[row][col].bloom_countdown = 1; } else if (choice == 2) { garden[row][col].flower = ROSE; garden[row][col].bloom_countdown = 2; } else if (choice == 3) { garden[row][col].flower = TULIP; garden[row][col].bloom_countdown = 3; } } print_garden(garden); } while (grow_garden(garden)) { printf("Waiting for flowers to bloom...\n"); print_garden(garden); } print_garden(garden); return 0; } /* Decreases the bloom_countdown for all flowers in the garden Parameters: garden: a 2D array of struct garden Returns: int: returns 1 if a flower has grown, 0 otherwise */ int grow_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]) { int has_grown = FALSE; for (int i = 0; i < GARDEN_ROWS; i++) { for (int j = 0; j < GARDEN_COLS; j++) { if (garden[i][j].bloom_countdown > 0) { garden[i][j].bloom_countdown--; has_grown = TRUE; } } } return has_grown; } //////////////////////////////////////////////////////////////////////////////// //////////////////// DO NOT MODIFY ANYTHING BELOW THIS LINE //////////////////// //////////////////////////////////////////////////////////////////////////////// /* Prints the garden to the terminal Parameters: garden: a 2D array of struct garden Returns: void */ void print_garden(struct garden garden[GARDEN_ROWS][GARDEN_COLS]) { printf("\n"); for (int i = 0; i < GARDEN_ROWS; i++) { for (int j = 0; j < GARDEN_COLS; j++) { if (garden[i][j].is_gardener) { printf("# "); } else if (garden[i][j].flower == EMPTY) { printf(". "); } else if (garden[i][j].bloom_countdown > 0) { printf("%d ", garden[i][j].bloom_countdown); } else if (garden[i][j].flower == DAISY) { printf("D "); } else if (garden[i][j].flower == ROSE) { printf("R "); } else if (garden[i][j].flower == TULIP) { printf("T "); } else { printf(". "); } } printf("\n"); } printf("\n"); }给我详细讲解这个代码
10-21
独立储能的现货电能量与调频辅助服务市场出清协调机制(Matlab代码实现)内容概要:本文围绕“独立储能的现货电能量与调频辅助服务市场出清协调机制”展开,提出了一种基于Matlab代码实现的优化模型,旨在协调独立储能系统在电力现货市场与调频辅助服务市场中的联合出清问题。文中结合鲁棒优化、大M法和C&CG算法处理不确定性因素,构建了多市场耦合的双层或两阶段优化框架,实现了储能资源在能量市场和辅助服务市场间的最优分配。研究涵盖了市场出清机制设计、储能运行策略建模、不确定性建模及求解算法实现,并通过Matlab仿真验证了所提方法的有效性和经济性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事电力市场、储能调度相关工作的工程技术人员。; 使用场景及目标:①用于研究独立储能在多电力市场环境下的协同优化运行机制;②支撑电力市场机制设计、储能参与市场的竞价策略分析及政策仿真;③为学术论文复现、课题研究和技术开发提供可运行的代码参考。; 阅读建议:建议读者结合文档中提供的Matlab代码与算法原理同步学习,重点关注模型构建逻辑、不确定性处理方式及C&CG算法的具体实现步骤,宜在掌握基础优化理论的前提下进行深入研读与仿真调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值