Maximal Discount

Description:

Linda is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, she goes completely mad and feels a need to buy all items in the store. You have given up on curing her for this disease, but try to limit its effect on her wallet. You have realized that the stores coming with these offers are quite selective when it comes to which items you get for free; it is always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing 400, 350, 300, 250, 200, 150, and 100 dollars, she will have to pay 1500 dollars. In this case she got a discount of 250 dollars. You realize that if she goes to the counter three times, she might get a bigger discount. E.g. if she goes with the items that costs 400, 300 and 250, she will get a discount of 250 the first round. The next round she brings the item that costs 150 giving no extra discount, but the third round she takes the last items that costs 350, 200 and 100 giving a discount of an additional 100 dollars, adding up to a total discount of 350. Your job is to find the maximum discount Linda can get.

Input:

The input consists of two lines. The first gives the number of items Linda is buying, 1 ≤ n ≤ 100. The next line gives the prices of these items, 1 ≤ pi ≤ 1000.

Output:

Output one line giving the maximum discount Linda can get by selectively choosing which items she brings to the counter at the same time.

Sample Input:

6

400 100 200 350 300 250

Sample Output:

400


Hint:

No Hint


全英的题目而且挺长的,是不是有点害怕呢?其实只是背景资料有点多~~

将数字排好序后,问题就变得很简单了呢~分析示例:红色圈起来的是可以作为折扣的价钱

原例:

排好序后:

发现,题目中给出的示例刚好是三的倍数,考虑,要不是三的倍数呢?

假如为3k+1,一个示例如下图:

3k+2类似


考虑先用数组存储,再排序。

通过上述分析,我们会发现,只要排好序,折扣的价钱分三种情况讨论即可。

但我们会发现,无论n = 3k或者n = 3k + 1或者n = 3k + 2,都只需要输出a[0]+a[3]+...+a[3k]即可。


我的代码:

#include<stdio.h>
int main() {
    int n, a[105], i, t, min, pi = 0, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for (i = 0; i < n-1; i++) {            // 对数组内的数进行排序~
        min = i;
        for (j = i + 1; j < n; j++)
        if (a[min] > a[j])
        min = j;
        if (min != i) {
            t = a[min];
            a[min] = a[i];
            a[i] = t;
        }
    }
    if (n % 3 == 0) {               // 分三类情况讨论(其实你们肯定发现了这没有必要。。。)
        for (i = 0; i < n; i += 3) {
            pi += a[i];
        }
    }
    else if (n % 3 == 1) {
        for (i = 1; i < n; i += 3) {
            pi += a[i];
        }
    }
    else if (n % 3 == 2) {
        for (i = 2; i < n; i += 3) {
            pi += a[i];
        }
    }
    printf("%d\n", pi);
    return 0;
}


标答:

#include<stdio.h>
#include<stdlib.h>

void Merge(int *R, int low, int m, int high);
void MergeSort(int R[], int low, int high);

int main(void) {
    int arr[110], num, i, j, res = 0;

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        scanf("%d", &arr[i]);
    }

    // sort the array with Merge Sort.
    MergeSort(arr, 0, num - 1);

    for (i = num - 1, j = 0; i >= 0; i--, j++) {
        if ((j + 1)%3 == 0) {
            res += arr[i];
        }
    }

    printf("%d\n", res);
    return 0;
}

void Merge(int *R, int low, int m, int high) {
    int i = low, j = m + 1, p = 0;
    int *R1;
    R1 = (int *)malloc((high - low + 1)*sizeof(i));   // 动态内存分配
    if (!R1) return;

    while (i <= m && j <= high) {
        R1[p++] = (R[i] <= R[j])?R[i++]:R[j++];    // a?b:c的含义是:当a为真时值为b,否则为c
    }

    while (i <= m) {
        R1[p++] = R[i++];
    }

    while (j <= high) {
        R1[p++] = R[j++];
    }

    for (p = 0, i = low; i <= high; p++, i++) {
        R[i] = R1[p];
    }

    free(R1);      // 用了malloc进行动态内存分配,当内存用完不再需要时需要将其释放
}

void MergeSort(int R[], int low, int high) {
    int mid;
    if (low < high) {
        mid = (low + high)/2;
        MergeSort(R, low, mid);
        MergeSort(R, mid + 1, high);
        Merge(R, low, mid, high);
    }
}


给标答加了一些注释,标答效率较高,用了归并排序(有兴趣可以百度~)

看了标答发现分三种情况输出是完全没有必要的,思维不够灵活。。。

时间: 2024-11-03 03:44:35

Maximal Discount的相关文章

Maximal Rectangle

题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 方法 使用两个矩阵,分别记录每一行连续的1的个数以及每一列连续的1的个数. public int maximalRectangle(char[][] matrix) { int lenX = matrix.length; if (lenX == 0) { r

hdu4104 Discount

给n个数,问最小不能组成的正整数是多少. 数学归纳法. 从sum=0开始,每次考察sum+1>=a[i],则sum+1肯定可以达到,那么sum+a[i]以内的数肯定可以达到. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <ve

LeetCode: Maximal Rectangle

LeetCode: Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

type parameters of &lt;T&gt;T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object

今天在进行代码检查的时候出现下面的异常: 1 type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object 当时的第一感觉就是代码因为jdk版本太低引起的. 因为最后咨询了配置管理组的同事,确实发现是因为我本地jdk用的是1.7版本,而代码检查机器上用的是jdk1.6版本.因此出现

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

84. Largest Rectangle in Histogram *HARD* 柱状图求最大面积 85. Maximal Rectangle *HARD*

1. Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The large

[LintCode] Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. Have you met this question in a real interview? Example For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0

lintcode 中等题:Maximal Square 最大子正方形

题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. 样例 For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题: 给定一个二维01矩阵,从中找出最大的全

Leetcode:Maximal Rectangle 最大全1子矩阵

Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 解题分析: 联想到 最大矩形面积 这一题,可以在O(n)时间内求出 最大的矩形面积 如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数. 利用 最大矩形面积 的方法,在O(n2)时间内就可以求出每一行形成的“柱状