UPCOJ 4201

来源:互联网 发布:淘宝店家一元拍 编辑:程序博客网 时间:2024/05/22 00:47

链接:

  http://exam.upc.edu.cn/problem.php?id=4201


题目:

题目描述

Teacher’s Day is coming, a group of n students decided to buy gifts.
The store offered them M gifts. The price is different for different gifts, buying the j-th gift costs pj yuan.
In total, the boys’ shared budget is a yuan.Besides, each of them has his own personal money, the i-th student has bi personal yuan.The shared budget can be spent on any student, but each boy’s personal money can be spent on buying only his gift.
Each boy can rent at most one gift, one cannot give his gift to sombody else.What maximum number of students will able to buy gifts?What minimum sum of personal money will they have to spend in total to let as many students buy gifts as possible?

输入

The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 1e5; 0 ≤ a ≤ 1e9). The second line contains the sequence of integers b1, b2, …, bn (1 ≤ bi ≤ 1e4), where bi is the amount of the i-th student’s personal money. The third line contains the sequence of integers p1, p2, …, pm (1 ≤ pj ≤ 1e9), where pj is the price for renting the j-th gift.

输出

Print two integers r and s, where r is the maximum number of students that can buy a gift and s is the minimum total personal money needed to buy r gifts. If the students cannot buy any gifts, then r = s = 0.

样例输入

2 2 10
5 5
7 6

样例输出

2 3


题意:

  有n个人去商店,商店里有m个物品,有a元的班费,除此之外第i个人自己的钱包里还有bi元,第j个物品的价格是pj元。

  问你最多能买多少件物品,在保证购买物品最多的情况下,所有人最少可以花多少自己钱包里的钱。


思路:

  对每个人拥有的钱和物品的价格排序一下,然后二分得到最多可以买到的物品数目k,然后将排序后的前k个物品的价格加和减去班费的钱,结果为负的话输出0就可以。


实现:

#include <iostream>#include <algorithm>#include <set>#include <string>#include <vector>#include <queue>#include <map>#include <stack>#include <list>#include <iomanip>#include <functional>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <cctype>//#define read read()#define edl putchar('\n')#define ll long long#define clr(a,b) memset(a,b,sizeof a)#define rep(i,m,n) for(ll i=m ; i<=n ; i++)#define fep(i,n) for(ll i=0 ; i<n ; i++)//inline ll read{ ll x=0;char c=getchar();while(c<'0' || c>'9')c=getchar();while(c>='0' && c<='9'){ x=x*10+c-'0';c=getchar(); }return x;}namespace FastIO {    const ll SIZE = 1 << 16;    char buf[SIZE], obuf[SIZE], str[60];    ll bi = SIZE, bn = SIZE, opt;    ll read(char *s) {        while (bn) {            for (; bi < bn && buf[bi] <= ' '; bi++);            if (bi < bn) break;            bn = fread(buf, 1, SIZE, stdin);            bi = 0;        }        ll sn = 0;        while (bn) {            for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];            if (bi < bn) break;            bn = fread(buf, 1, SIZE, stdin);            bi = 0;        }        s[sn] = 0;        return sn;    }    bool read(ll& x) {        ll n = read(str), bf;        if (!n) return 0;        ll i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1;        for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';        if (bf < 0) x = -x;        return 1;    }};#define read(x) FastIO::read(x)using namespace std;const ll maxn = int(1e5)+7;ll n, m, a, p[maxn], cost[maxn];bool check(ll aim) {    if(aim > m) return false;    ll tmp = 0;    for(ll i=0, j = n-aim ; i<aim ; i++, j++) {        if(cost[i] > p[j]) tmp += cost[i] - p[j];    }    return tmp <= a;}int main() {#ifndef ONLINE_JUDGE    freopen("../1.in", "r", stdin);//    printf("init! init! init!\n");#endif    while(read(n)&&read(m)&&read(a)) {        fep(i,n) read(p[i]);        fep(i,m) read(cost[i]);        sort(p,p+n);        sort(cost,cost+m);        ll l = 0, r = n, mid, ans = 0;        while(l <= r) {            mid = l+r>>1;            if(check(mid)) {                ans = mid;                l = mid+1;            } else r = mid-1;        }        printf("%lld ", ans);        if(ans == 0) {            puts("0");            continue;        }        ll result = 0;        for(ll i=0 ; i<ans ; i++) {            result += cost[i];        }        printf("%lld\n",result - a);    }    return 0;}
原创粉丝点击