浙大pat 1037
1037. Magic Coupon (25)
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have some coupons with negative N's!
For example, given a set of coupons {1 2 4 -1}, and a set of product values {7 6 -2 -3} (in Mars dollars M$) where a negative value corresponds to a bonus product. You can apply coupon 3 (with N being 4) to product 1 (with value M$7) to get M$28 back; coupon 2 to product 2 to get M$12 back; and coupon 4 to product 4 to get M$3 back. On the other hand, if you apply coupon 3 to product 4, you will have to pay M$12 to the shop.
Each coupon and each product may be selected at most once. Your task is to get as much money back as possible.
Input Specification:
Each input file contains one test case. For each case, the first line contains the number of coupons NC, followed by a line with NC coupon integers. Then the next line contains the number of products NP, followed by a line with NP product values. Here 1<= NC, NP <= 105, and it is guaranteed that all the numbers will not exceed 230.
Output Specification:
For each test case, simply print in a line the maximum amount of money you can get back.
Sample Input:
4
1 2 4 -1
4
7 6 -2 -3
Sample Output:
43 。。。。。。。。。。。。。。。。我的代码的也很乱
#include"iostream"
#include "algorithm"
#include "string"
#include "vector"
using namespace std;
#define max 100001
int nc[max],np[max];
bool mycompare(int a, int b)
{
return a>b;
}
int main()
{
int c,p,sum=0;
cin >> c;
for(int i=0;i<c;i++)
cin >> nc[i];
sort(nc,nc+c,mycompare);
cin >> p;
for(int i=0;i<p;i++)
cin >> np[i];
sort(np,np+p,mycompare);
int min = c<p? c:p;
int mx = c>p? c:p;
if(mx == min)
{
for(int i=0;i<min;i++)
if(nc[i]*np[i]>=0)
sum +=np[i]*nc[i];
if(sum==0)
sum=np[0]*nc[min-1]>np[min-1]*nc[0]?np[0]*nc[min-1]:np[min-1]*nc[0];//考虑 两个数组符号全部不相同时就去一个数最小的数
}
else
{
if(c==min)
{
for(int i=0;i<c;i++)
{
if(nc[i]*np[i]>=0)
sum +=nc[i]*np[i];
}
if(sum==0)
sum =np[0]*nc[min-1]>np[mx-1]*nc[0]?np[0]*nc[min-1]:np[mx-1]*nc[0];
}
else if(c==mx)
{
for(int i=0;i<p;i++)
{
if(nc[i]*np[i]>=0)
sum +=nc[i]*np[i];
}
if(sum==0)
sum =np[0]*nc[mx-1]>np[min-1]*nc[0]?np[0]*nc[mx-1]:np[min-1]*nc[0];
}
}
cout << sum <<endl;
return 0;
}
浙大pat 1037的更多相关文章
- 浙大 PAT 乙级 1001-1075 目录索引
1001. 害死人不偿命的(3n+1)猜想 1002. 写出这个数 1003. 我要通过! 1004. 成绩排名 1005. 继续(3n+1)猜想 1006. 换个格式输出整数 1007. 素数对猜想 ...
- A题进行时--浙大PAT 1001-1010
pat链接:http://pat.zju.edu.cn 1 #include<stdio.h> 2 int main(){ 3 int a,b; 4 int c; 5 while(scan ...
- 浙大pat 1035题解
1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...
- 浙大pat 1025题解
1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...
- PAT 1037 在霍格沃茨找零钱(20)(代码+思路)
1037 在霍格沃茨找零钱(20)(20 分) 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 -- 就如海格告诉哈利的:"十七个银西可(Sickle)兑一个加隆(Galleon) ...
- PAT——1037. 在霍格沃茨找零钱
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.”现在,给定 ...
- PAT 1037 Magic Coupon[dp]
1037 Magic Coupon(25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an in ...
- PAT 1037. 在霍格沃茨找零钱(20)
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 -- 就如海格告诉哈利的:"十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.& ...
- A题进行时--浙大PAT 1021-1030
1021: #include<stdio.h> #include<string.h> #include<vector> #include<queue> ...
随机推荐
- Cg(C for Graphic)标准函数库之数学函数与几何函数
和 C 的标准函数库类似, Cg 提供了一系列内建的标准函数.这些函数用于执行数学上的通用计算或通用算法(纹理映射等),例如,需要求取入射光线的反射光线方向向量可以使用标准函数库中的 reflect ...
- 单机Hadoop搭建
通过一段时间的学习,我在我的centos上安装了单机hadoop,如果这对你有帮助,就进来探讨学习一下 Hadoop伪分布式配置 Hadoop 可以在单节点上以伪分布式的方式运行,Hadoop 进程以 ...
- C# 语言规范_版本5.0 (第21章 附录C_参考资料)
A. 参考资料 Unicode 联合会.The Unicode Standard, Version 3.0(Unicode 标准 3.0 版).Addison-Wesley,Reading,Massa ...
- 著名清理软件(CCleaner) 5.24.5841 中文版
软件名称: 著名清理软件(CCleaner) 软件语言: 多国语言 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 5.7MB 图片预览: 软件简介: CCleaner的体积小, ...
- 在ueditor编辑器的光标停留处插入内容
业务场景: 首先在ueditor编辑器中插入一段文本,然后我想在文本的某个位置(光标停留处)插入一个字符串,这个字符串是从页面的其他地方选择得来的. 注意,当我们点击ueditor编辑器以外的地方,编 ...
- 升级ruby
1.安装 RVM RVM:Ruby Version Manager,Ruby版本管理器,包括Ruby的版本管理和Gem库管理(gemset) $ curl -L get.rvm.io | bash - ...
- tar命令结合find搜索将指定条件的文件进行打包压缩
今天处理 /tmp 临时目录下 session 文件过多的问题,顺便找到了这个 tar 结合 find 打包压缩的方法,记录下来,很实用的哈! 搜索 /tmp 目录下 创建超过 24 分钟的文件 fi ...
- 基于React Native的58 APP开发实践
React Native在iOS界早就炒的火热了,随着2015年底Android端推出后,一套代码能运行于双平台上,真正拥有了Hybrid框架的所有优势.再加上Native的优秀性能,让越来越多的公司 ...
- 使用automake等命令自动生成Makefile文件
参考:http://www.cnblogs.com/njucslzh/archive/2010/04/29/1723320.html
- <T> List<T>前面<T>的意思
先看例子: import java.util.*; class Fruit { public String toString() { return "Fruit"; } } cla ...