trees in a row

本文介绍了一道有趣的算法题目:为了满足英国女王的要求,皇家园林师需要调整花园中树木的高度,使得相邻两棵树的高度差固定为某个数值。文章提供了算法思路及实现代码,通过遍历树的高度并计算最小的操作次数来达成目标。


The Queen of England has n trees growing in a row in her garden. At that, the i-th (1?≤?i?≤?n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1?≤?i?<?n), ai?+?1?-?ai?=?k, where k is the number the Queen chose.

Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?


Input

The first line contains two space-separated integers: n, k (1?≤?n,?k?≤?1000). The second line contains n space-separated integers a1,?a2,?...,?an (1?≤?ai?≤?1000) — the heights of the trees in the row.


Output

In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions.

If the gardener needs to increase the height of the j-th (1?≤?j?≤?n) tree from the left by x (x?≥?1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1?≤?j?≤?n) tree from the left by x (x?≥?1) meters, print on the corresponding line "- j x".

If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.


Sample test(s)

 

Input
4 1

1 2 1 5


Output
2

+ 3 2

- 4 1


Input
4 1

1 2 3 4


Output
0


Source:

 

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    freopen("treerow_testcases", "r", stdin);
    int caseNum;
    cin >> caseNum;
    while(caseNum--)
    {
        int treeNum,tolerance;
        cin >> treeNum >> tolerance;
        int *tree = new int [treeNum];
        for(int i = 0; i < treeNum; i++)
        {
            cin >> tree[i];
        }

        //
        int minChange = treeNum;
        int base = 0;
        for(int i = 0; i < treeNum; i++)
        {
            int changeNum = 0;              // trees to change
            int height = tree[i] - i*tolerance; // the first tree's height after changed
            if(height <= 0) continue;       // tree's height can't be zero
            // chose the i as the base, count the trees need to change
            for(int j = 0; j < treeNum; j++)
            {
                if(height != tree[j])
                {
                    changeNum++;
                }
                height += tolerance;
            }

            if(changeNum == 0)
            {
                base = i;
                minChange = changeNum;
                break;
            }
            else if(changeNum < minChange)
            {
                base = i;
                minChange = changeNum;
            }
        }

        cout << minChange << endl;
        int height = tree[base] - base*tolerance;
        for(int i = 0; i < treeNum; i++)
        {
            if(height != tree[i])
            {
                cout << ((height > tree[i])? "+":"-") << i+1 << " ";
                cout << ((height > tree[i])? (height - tree[i]) : (tree[i] - height)) << endl;
            }
            height += tolerance;
        }
        //

        delete [] tree;
    }
    return 0;
}


 

帮我写一个脚本,我现在在 (base) root@dev-kunlin-yang-aieditor-test-je93-849d767db7-pr72x:/common-data/kunlin/code/ReCamMaster-main# ls /common-data/kunlin/code/ReCamMaster-main/split_metadata/ metadata_1.csv metadata_2.csv metadata_4.csv metadata_6.csv metadata_8.csv metadata_10.csv metadata_3.csv metadata_5.csv metadata_7.csv metadata_9.csv 其中每一个csv是:file_name text 001_lgFzrIujya8_13_19to172.mp4 The video features a woman named Terri Thomas, who is 50 years old and a Fancidancer. She is wearing a pink headband with a pink ribbon design, a pink shirt, and a gold necklace. The video is shot in a room with a clock on the wall. The style of the video is a close-up interview, with Terri speaking directly to the camera. The focus is on her face and upper body, and the room provides a simple, uncluttered background. The lighting is bright and even, highlighting Terri's features and the colors of her clothing. The overall impression is of a confident and articulate individual sharing her story. 004_Hxc1AXTUzA4_11_0to111.mp4 In the video, a man in a green shirt is standing in a kitchen, gesturing with his hands as he speaks. He is pointing towards a wooden cutting board on a counter. The kitchen is well-equipped with various appliances and items, including a refrigerator, an oven, and several potted plants. There are also bowls and a bottle on the counter. The man appears to be explaining something, possibly related to cooking or food preparation, as he is in a kitchen setting. The overall style of the video seems to be informative or instructional, with the man providing guidance or commentary on the scene. 005_CQsjrgfCF9I_8_0to145.mp4 The video features a man standing amidst a lush garden filled with various potted plants. The man, dressed in a plaid shirt, is positioned in the center of the frame, surrounded by greenery. He appears to be speaking or presenting, as suggested by the text overlay on the video. The text, which reads "Let the soil dry between waterings", is likely providing gardening advice or tips. The overall style of the video seems to be informative, possibly aimed at gardening enthusiasts or those interested in plant care. The setting, with its abundance of plants, creates a serene and natural atmosphere, further emphasizing the gardening theme. 006_bpfqYgCvutY_38_0to147.mp4 In the video, a man and a woman are engaged in a conversation in a fresh produce market. The man, dressed in a pink shirt, is leaning over a barrel filled with fresh fruits and vegetables. The woman, wearing a black shirt, stands across from him, attentively listening to his words. The market is well-stocked with various fruits and vegetables, including apples, oranges, and bananas, all displayed in wooden barrels. A shopping cart is also visible in the scene, suggesting that the man and woman might be customers. The overall atmosphere of the video is lively and colorful, reflecting the abundance of fresh produce in the market. 008_LiBGM38mwh4_0_0to106.mp4 The video shows a small, rustic hut with a thatched roof, situated in a grassy field. The hut is adorned with an abundance of pumpkins and gourds, arranged in a decorative manner around the entrance and sides of the structure. The pumpkins and gourds vary in size and color, creating a vibrant and festive display. The hut is surrounded by a lush green lawn, and in the background, there are trees and a house. The overall style of the video is whimsical and seasonal, capturing the essence of a fall harvest celebration. 我希望你能够按照这里filename去/common-data/kunlin/code/ReCamMaster-main/selected_videos/拿对应的视频组成文件夹test_videoX, metadata1就是test_video1,等等,同时将视频放在test_videoX下面的videos,将metadata放在test_videoX下面,再将"/common-data/kunlin/code/ReCamMaster-main/example_test_data/cameras/" 复制到每个test_video下面
最新发布
09-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值