http://pat.zju.edu.cn/contests/pat-practise/1037

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
[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<vector>  
  9. #include<cstdlib>  
  10. #include<cmath>  
  11. #include<iomanip>  
  12. #include<string>  
  13. using namespace std;  
  14. #define MAX 10000000000  
  15. int a1[100001],a2[100001];  
  16. int b1[100001],b2[100001];  
  17. long long c[100001];  
  18. void add(long long num){  
  19.     c[0] += num;  
  20.     for(int i=0;1;++i){  
  21.         if(c[i]>=MAX){  
  22.             c[i+1] += c[i]/MAX;  
  23.             c[i] %= MAX;  
  24.         }else if(c[i]==0){  
  25.             break;  
  26.         }  
  27.     }  
  28. }  
  29. bool cmp(int a, int b){  
  30.     return a>b;  
  31. }  
  32. int main(){  
  33.   
  34.    // freopen("in.txt", "r", stdin);  
  35.     int t;  
  36.     int n;  
  37.     int n1,n2;  
  38.     int m;  
  39.     int m1, m2;  
  40.     cin>>n;  
  41.     n1=0,n2=0;  
  42.     for(int i=0;i<n;++i){  
  43.         scanf("%d", &t);  
  44.         if(t>0)  
  45.             a1[n1++] = t;  
  46.         else if(t<0)  
  47.             a2[n2++] = t;  
  48.     }  
  49.   
  50.     cin>>m;  
  51.     m1=0,m2=0;  
  52.     for(int i=0;i<m;++i){  
  53.         scanf("%d", &t);  
  54.         if(t>0)  
  55.             b1[m1++] = t;  
  56.         else if(t<0)  
  57.             b2[m2++] = t;  
  58.     }  
  59.   
  60.     memset(c, 0, sizeof(c));  
  61.     sort(a1, a1+n1, cmp);  
  62.     sort(b1, b1+m1, cmp);  
  63.     int len = n1>m1?m1:n1;  
  64.     long long tmp;  
  65.     for(int i=0;i<len;++i){  
  66.         tmp = (long long)(a1[i]*b1[i]);  
  67.         add(tmp);  
  68.     }  
  69.   
  70.     sort(a2, a2+n2);  
  71.     sort(b2, b2+m2);  
  72.     len = n2>m2?m2:n2;  
  73.     for(int i=0;i<len;++i){  
  74.         tmp = (long long)(a2[i]*b2[i]);  
  75.         add(tmp);  
  76.     }  
  77.   
  78.     int k = 999;  
  79.     bool first = true;  
  80.     while(k>=0){  
  81.         if(c[k]==0){  
  82.             k--;  
  83.             continue;  
  84.         }  
  85.           
  86.         if(first){  
  87.             printf("%lld", c[k]);  
  88.             first = false;  
  89.         }  
  90.         else  
  91.             printf("%10lld", c[k]);  
  92.         k--;  
  93.   
  94.     }  
  95.     if(first)  
  96.         printf("0");  
  97.     printf("\n");  
  98.       
  99.       
  100.     //fclose(stdin);  
  101.     return 0;  
  102. }  
  103.           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值