1037. Magic Coupon (25)

时间限制
100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. 浙大 PAT 乙级 1001-1075 目录索引

    1001. 害死人不偿命的(3n+1)猜想 1002. 写出这个数 1003. 我要通过! 1004. 成绩排名 1005. 继续(3n+1)猜想 1006. 换个格式输出整数 1007. 素数对猜想 ...

  2. 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 ...

  3. 浙大pat 1035题解

    1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...

  4. 浙大pat 1025题解

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  5. PAT 1037 在霍格沃茨找零钱(20)(代码+思路)

    1037 在霍格沃茨找零钱(20)(20 分) 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 -- 就如海格告诉哈利的:"十七个银西可(Sickle)兑一个加隆(Galleon) ...

  6. PAT——1037. 在霍格沃茨找零钱

    如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.”现在,给定 ...

  7. PAT 1037 Magic Coupon[dp]

    1037 Magic Coupon(25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an in ...

  8. PAT 1037. 在霍格沃茨找零钱(20)

    如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 -- 就如海格告诉哈利的:"十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.& ...

  9. A题进行时--浙大PAT 1021-1030

    1021: #include<stdio.h> #include<string.h> #include<vector> #include<queue> ...

随机推荐

  1. python爬虫框架scrapy初试(二点一)

    功能:爬取某网站部分新闻列表和对应的详细内容. 列表页面http://www.zaobao.com/special/report/politic/fincrisis 实现代码: import scra ...

  2. ECMAScript6之数值类型的扩展

    数值类型的扩展 Number.isNaN ES6将isNaN方法从window身上移植到了Number对象上,使用时和ES5中的isNaN方法一样,但是这是Number对象的方法 Number.isN ...

  3. 关于高性能javascript 笔记

    最近买了本新书,准备自己吃 狗粮的同时也吃点精神食粮.笔记总结,从现在开始,看我啥时候能看完这本酥,就酱紫, begin:

  4. MySQL连接无法解析HOST主机名

    #1042 - Can't get hostname for your address 使用IP链接或域名链接都可能遇到这个问题 解决办法: my.ini 或 my.cnf 末尾添加 skip-nam ...

  5. dev Gridcontrol单元格值格式化及在模板中调用命令

    <dxg:GridColumn>                    <dxg:GridColumn.EditSettings>                        ...

  6. NodeJs md5 sha1加密

    var crypto = require('crypto');module.exports = {  md5: (str)=> {    return crypto.createHash('md ...

  7. div显示与隐藏及height()函数

    总结与网络 1. $("#id").show()表示display:block,$("#id").hide()表示display:none; $("# ...

  8. webstorm之js,css文件压缩

    不说废话了,代码压缩是每个网站上线前的必备过程,但由于有时候小的项目或者加急的项目每次都构建一次类似gulp或者grunt等前端部署,实在是大题小做,所以才有了今天这个帖子: 我们会用到yui com ...

  9. hibernate------->第一个程序

    今天学习hibernate . 创建User类: package com.hibernate; import java.util.Date; public class User { private I ...

  10. OOP的字段

    <?php class Archie{ //字段成员声明格式:修饰符 变量名 [=xxx] public $_name='Archie!'; //public表示共有,类外可以访问 public ...