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[], i, t, min, pi = , j;
scanf("%d", &n);
for (i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = ; i < n-; i++) { // 对数组内的数进行排序~
min = i;
for (j = i + ; 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 % == ) { // 分三类情况讨论(其实你们肯定发现了这没有必要。。。)
for (i = ; i < n; i += ) {
pi += a[i];
}
}
else if (n % == ) {
for (i = ; i < n; i += ) {
pi += a[i];
}
}
else if (n % == ) {
for (i = ; i < n; i += ) {
pi += a[i];
}
}
printf("%d\n", pi);
return ;
}

标答:

#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[], num, i, j, res = ; scanf("%d", &num);
for (i = ; i < num; i++) {
scanf("%d", &arr[i]);
} // sort the array with Merge Sort.
MergeSort(arr, , num - ); for (i = num - , j = ; i >= ; i--, j++) {
if ((j + )% == ) {
res += arr[i];
}
} printf("%d\n", res);
return ;
} void Merge(int *R, int low, int m, int high) {
int i = low, j = m + , p = ;
int *R1;
R1 = (int *)malloc((high - low + )*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 = , 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)/;
MergeSort(R, low, mid);
MergeSort(R, mid + , high);
Merge(R, low, mid, high);
}
}

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

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

Maximal Discount的更多相关文章

  1. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  2. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  3. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  4. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  5. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

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

    今天在进行代码检查的时候出现下面的异常: type parameters of <T>T cannot be determined; no unique maximal instance ...

  7. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  8. [LintCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

随机推荐

  1. BestCoder4 1002 Miaomiao's Geometry (hdu 4932) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1 ...

  2. Java中的switch语句

    switch可以替代if..else..,另外据说switch采用二分搜索,效率会更高一点. switch(type) { case 1 : type_name="INCOMING" ...

  3. [Selenium] Selenium common Actions Examples

    1.sendKeys() 在文本框中输入字符串 WebElement searchBox = driver.findElement(By.name("q")); searchBox ...

  4. Watir: Watir webdriver对JS 弹出框的操作现在非常简单。

    以下代码支持Firefox,IE,Chrome require 'watir-webdriver' #require "watir-webdriver/extensions/alerts&q ...

  5. laravel的核心概念:服务提供者provider解析

    我们知道laravel和核心就是一个IoC容器, 被称之为服务容器. 那么作为IoC容器, 就必须要有绑定对象生成器的工作.  在laravel中是服务提供者来项服务容器中绑定对象生成器的. 百牛信息 ...

  6. ThreadPoolExecutor之二:jdk实现的线程池介绍

    一 简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后加入了java.uti ...

  7. 考拉定时任务框架kSchedule

    此文已由作者杨凯明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.背景 目前项目中使用的定时任务框架存在下面这些问题 没有统一的定时任务管理平台 目前项目中使用定时任务的 ...

  8. IntentService使用以及源码分析

    一 概述 我们知道,在Android开发中,遇到耗时的任务操作时,都是放到子线程去做,或者放到Service中去做,在Service中开一个子线程来执行耗时操作. 那么,在Service里面我们需要自 ...

  9. c# KeyDown KeyPress 函数中event 的 Handled属性

    很奇怪的 KeyDown中的 Handled.true 只能使 Keys.Back 这类失效, 如果要使比如数字失效,必须设置一个变量 _bHandled = true 然后在紧接着会触发的 KeyP ...

  10. 如何才能优雅地书写JS代码

    第一:关于匿名函数的使用 要避免全局变量泛滥, 可以考虑使用匿名函数, 把不需要在外部访问的变量或者函数限制在一个比较小的范围内. 例如以下代码: <script> function fu ...