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



我的代码:
#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的更多相关文章
- [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 ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 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 ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [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 ...
- [LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...
随机推荐
- devm_regmap_init_i2c【转】
本文转载自:http://blog.csdn.net/u011975319/article/details/52128845 本文有此处转载http://blog.csdn.net/luckywang ...
- POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()
题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total ...
- Git 对比两分支中同一文件
语法 git diff <分支名> <分支名> -- 文件名 git diff branch1 branch2 -- path/file.txt 案例 git diff ori ...
- cassandra cpp driver中bind list——用cass_statement_bind_collection函数
CassError insert_into_collections(CassSession* session, const char* key, const char* items[]) { Cass ...
- Opencv函数setMouseCallback鼠标事件响应
用户通过鼠标对图像视窗最常见的操作有: 1. 左键单击按下 2. 左键单击抬起 3. 左键按下拖动 4. 鼠标指针位置移动 单次单击操作响应事件及顺序 Opencv中setMouseCallback( ...
- Laravel中常见的错误与解决方法小结
一.报错: 「Can't swap PDO instance while within transaction」 通过查询 Laravel 源代码,可以确认异常是在 setPdo 方法中抛出的: ? ...
- SVN进行代码的托管
svn 使用的是集中服务器 就是只有一个服务器的意思 git 是分布式服务器 服务器: 存储客户端上传的源代码. 可以在Windows上通过安装 Visual SVN Sever . 客户端: 上 ...
- windows动态磁盘导致的分区问题
上次说到由于装双系统导致我的win7启动不了了,一直以为是不是在ubuntu的安装界面点错了什么东西导致的,甚至认为是不是server的安装程序有点bug,直到今天继续折腾才发现了问题所在,跟ubun ...
- ASP.NET Core MVC 2.x 全面教程__ASP.NET Core MVC 19. XSS & CSRF
存库之前先净化,净化之后再提交到数据库 刚才插入的那笔数据 把默认的Razor引擎默认的EnCode去掉.Razor默认会开启htmlEnCodding 数据恢复回来 插入数据库之前对插入的数据进行净 ...
- 博客使用的CSS代码备份
CSS代码备份 /*simplememory*/ #google_ad_c1, #google_ad_c2 { display: none; } .syntaxhighlighter a, .synt ...