Trees in a Row(暴力)

B. Trees in a Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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: nk (1 ≤ n, k ≤ 1000). The second line contains n space-separated integersa1, 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

 

    题意:

    给出 N,K(1 ~ 1000)。代表有N个数,输出要如何操作最少数,使整个序列满足 ai - ai-1 = k。输出最少操作数,后输出这些操作是什么。(操作  + 序号 + 个数)。每个数都必须 >= 0。

 

    思路:

    暴力。1000个数,每个数都扫一遍也就O(n^2)< 1s,果断暴搜。每个位置都向两边搜索一遍,同时比较最小操作数,若有数小于等于0则不取这个点。一开始没看到所有数都必须 >= 0,无限wa。 

 

    AC:

#include <stdio.h>
#include <string.h>

int num[1005],vis[1005];

int main() {
    int n,k,step = 9999,in;
    scanf("%d%d",&n,&k);
    for(int i = 1;i <= n;i++)
        scanf("%d",&num[i]);

    for(int i = 1;i <= n;i++) {
        int t = i,ans = 0,temp = 1;
        vis[t] = num[t];
        while(t != 1) {
            if(num[t - 1] + k != vis[t]) ans++;
            vis[t - 1] = vis[t] - k;
            if(vis[t - 1] <= 0) {
                temp = 0;
                break;
            }
            t--;
        }

        if(!temp) continue;
        t = i;
        while(t != n) {
            if(vis[t] + k != num[t + 1]) ans++;
            vis[t + 1] = vis[t] + k;
            t++;
        }

        if(ans < step) {
            step = ans;
            in = i;
        }
    }

    printf("%d\n",step);
    int t = in;
    while(t != 1) {
        int ans = num[t - 1] + k - num[t];
        if(ans > 0) printf("- %d %d\n",t - 1,ans);
        if(ans < 0) printf("+ %d %d\n",t - 1,-ans);
        num[t - 1] = num[t] - k;
        t--;
    }

    t = in;
    while(t != n) {
        int ans = num[t] + k - num[t + 1];
        if(ans > 0) printf("+ %d %d\n",t + 1,ans);
        if(ans < 0) printf("- %d %d\n",t + 1,-ans);
        num[t + 1] = num[t] + k;
        t++;
    }

    return 0;
}

 

### 如何使用二叉搜索树(BST)实现 A+B 操作 在 C 编程语言中,可以通过构建两个二叉搜索树(BST),分别表示集合 A 和 B 的元素,然后通过遍历其中一个 BST 并将其节点插入到另一个 BST 中来完成 A+B 操作。以下是详细的实现方法: #### 数据结构定义 首先需要定义一个简单的二叉搜索树节点的数据结构。 ```c typedef struct TreeNode { int value; struct TreeNode* left; struct TreeNode* right; } TreeNode; ``` #### 插入函数 为了向 BST 添加新元素,可以编写如下 `insert` 函数。 ```c TreeNode* createNode(int value) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->value = value; newNode->left = NULL; newNode->right = NULL; return newNode; } void insert(TreeNode** root, int value) { if (*root == NULL) { *root = createNode(value); } else { if (value < (*root)->value) { insert(&((*root)->left), value); // Insert into the left subtree. } else if (value > (*root)->value) { insert(&((*root)->right), value); // Insert into the right subtree. } // If value == (*root)->value, do nothing since duplicates are not allowed in a set. } } ``` #### 合并操作 要执行 A+B 操作,即合并两棵 BST,可以从一棵树中提取所有元素并将它们逐个插入另一棵树中。 ```c // In-order traversal to extract elements from one tree and add them to another. void mergeTrees(TreeNode* sourceRoot, TreeNode** targetRoot) { if (sourceRoot != NULL) { mergeTrees(sourceRoot->left, targetRoot); // Traverse left subtree first. insert(targetRoot, sourceRoot->value); // Add current node's value to target tree. mergeTrees(sourceRoot->right, targetRoot); // Then traverse right subtree. } } ``` #### 主程序逻辑 假设我们已经初始化了两棵 BST 表示集合 A 和 B,则可以通过调用上述函数完成 A+B 操作。 ```c int main() { TreeNode* treeA = NULL; TreeNode* treeB = NULL; // Example: Adding values to Tree A. int arrayA[] = {5, 3, 7, 2, 4}; for (size_t i = 0; i < sizeof(arrayA)/sizeof(arrayA[0]); ++i) { insert(&treeA, arrayA[i]); } // Example: Adding values to Tree B. int arrayB[] = {6, 8, 1}; for (size_t i = 0; i < sizeof(arrayB)/sizeof(arrayB[0]); ++i) { insert(&treeB, arrayB[i]); } // Perform A + B by merging all nodes of treeB into treeA. mergeTrees(treeB, &treeA); // Now treeA contains all unique elements from both sets. return 0; } ``` 此代码片段展示了如何利用二叉搜索树的性质高效地进行集合并集运算[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值